Python

python + vscode = 이미지 크롤링-2

85chong 2021. 8. 2. 18:42
728x90
반응형
SMALL
import urllib.request
import lxml.html
from bs4 import BeautifulSoup, element


pageNum = 1

#저장할 이미지 경로 및 이름 (data폴더(없으면 만들어놓을것)에 face0.jpg 형식으로 저장)
imageNum0 = 0
imageNum = 0
imageNum2 = 0
imageStr = "test_data/img"

#url = "https://www.kr.playblackdesert.com/BeautyAlbum?searchType=0&searchText=&categoryCode=0&classType=0,4,8,12,16,20,21,24,25,26,28,31,27,19,23,11,29,17,5&Page="
base_url = "http://www.toybox.kr/shop/shopdetail.html?branduid=1731026&xcode=053&mcode=004&scode=&type=Y&search=&sort=brandname"
    
url = base_url + str(pageNum)
    
fp = urllib.request.urlopen(url)
source = fp.read();
fp.close()

soup = BeautifulSoup(source, 'html.parser')
soup = soup.findAll("td",class_ = "woong")

for i in soup :
        imgURL = i("img")

        if len(imgURL)==0 :
           print("none")
        else : 
           for j in imgURL :
               try : 
                   resultURL = imgURL[imageNum2]["src"]
                   print("resultURL : ",resultURL)
                   urllib.request.urlretrieve(resultURL,imageStr+str(imageNum2)+".jpg")
                   imageNum2+=1
                    
               except Exception as e: 
                   print("excep : ",e)
                   imageNum2 = 0

        imageNum+=1