파이썬으로 네이버 이미지 크롤링하는 크롤러 구현하기
월드플레이의 간단한(?)게임을 구현하기 위해 필요한 인공지능 웹앱을 만들어보려고
from urllib.request import urlopen
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
baseUrl = 'https://search.naver.com/search.naver?where=image&sm=tab_jum&query='
plusUrl = input('검색어를 입력하세요 : ')
# 한글 검색 자동 변환
url = baseUrl + quote_plus(plusUrl)
html = urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
img = soup.find_all(class_='_img')
print(img)
n = 1
for i in img:
imgUrl = i['data-source']
with urlopen(imgUrl) as f:
with open(plusUrl + str(n) + '.jpg', 'wb') as h:
img = f.read()
h.write(img)
n += 1
print(imgUrl)
print('다운로드 완료')