Python

python + vscode = 이미지 크롤링-2(upgrade ver)

85chong 2021. 8. 2. 22:58
728x90
반응형
SMALL

* 필수 설치 

cmd > 

pip install requests
pip install bs4
pip install os
pip install lxml

 

import urllib.request
import os
import lxml.html
from bs4 import BeautifulSoup, element

pageNum = 1
imageNum0 = 0
imageNum = 0
imageNum2 = 0
imageStr = "C:/WebScrapingImage/"


#시작하는 func 생성
def start():

    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"]
                        createFolder()
                        urllib.request.urlretrieve(resultURL,imageStr+str(imageNum2)+".jpg")
                        imageNum2+=1
                    except Exception as e: 
                        print("excep : ",e)
                        imageNum2 = 0
            global imageNum
            imageNum+=1


#폴더 생성하는 func 생성
def createFolder():
    try:
        if not os.path.exists(imageStr):
            os.makedirs(imageStr)
    except OSError:
        print("Error creating dir")                        




#함수 시작
start()

'Python' 카테고리의 다른 글

Python 폴더 삭제후 재설치 for MAC  (1) 2024.05.29
Python 실행파일 만들기  (0) 2021.08.02
python + vscode = 이미지 크롤링-2  (0) 2021.08.02
python 크롤링 html type  (0) 2021.07.30
python + vscode = 이미지 크롤링  (0) 2021.07.30