Coding Study/Python
G마켓 - 베스트 카테고리 & 상품상세 공급사 정보 크롤링하기
Yu_Jayden
2020. 11. 26. 10:53
728x90
반응형
오늘은 2일 전에 포스팅했던 G마켓 베스트 100 > 패션의류 카테고리 페이지의 상품명과 가격을 크롤링하고 해당 상품의 상품 상세 페이지의 공급사 정보까지 추가적으로 크롤링하는 소스를 작성해 보도록 하겠습니다. 아직 파이썬 초보 개발자로서 제가 공부하고 코딩해 본 소스에 대해 기록을 남기는 차원입니다.
import requests
from bs4 import BeautifulSoup
res = requests.get('http://corners.gmarket.co.kr/Bestsellers?viewType=G&groupCode=G01')
soup = BeautifulSoup(res.content, 'html.parser')
data = soup.select('div.best-list')
dataitems = data[1]
products = dataitems.select('ul > li')
for index, product in enumerate(products):
title = product.select_one('a.itemname')
price = product.select_one('div.s-price > strong')
res_info = requests.get(title['href'])
# itemname class의 href 태그 데이터를 res_info 변수에 저장
soup_info = BeautifulSoup(res_info.content, 'html.parser')
provider_info = soup_info.select_one('div.item-topinfo > div.item-topinfo_headline > p > a > strong')
# 공급사명 데이터 추출
print(str(index + 1)+'.', title.get_text(), price.get_text(), provider_info.get_text())
# 상품명앞에 번호를 추가하기 위해 index 변수를 1씩 증가시켜 문자열로 type 변환
# provider_info 는 상품상세페이지의 공급사명 데이터를 가지고 있는 변수를 출력
728x90
반응형