크롤링으로 옥션베스트의 상품정보(상품명,원래가격,할인가격,상품이미지)를 가져와 딕셔너리로 출력하는 예제
페이지 정보
작성자 관리자 댓글 0건 조회 239회 작성일 22-01-05 00:29본문
import requests
from bs4 import BeautifulSoup
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'}
ls_req = requests.get('http://corners.auction.co.kr/corner/categorybest.aspx', headers=header, verify=False)
ls_html = ls_req.text
ls_parse = BeautifulSoup(ls_html, 'html.parser')
#파서된 태그중에 div의 클래스가 rank인 것의 개수를 ls_count에 담아둠
ls_count = ls_parse.find_all("div", {"class": "rank"})
#print(len(ls_count))
#딕셔너리를 담아둘 변수선언
temp_dict = {}
#태그중에 할인가격은 있지만 원래 가격이 없는 경우가 있는데 그런경우에는
#원래 가격은 공백으로 변수에 담고 배열은 -1된 원래 가격을 출력해주기위해 j를 사용
j=0
#for i in range(len(ls_count)): 목록에 있는 전체개수200개를 조회
#전체목록중 10개만 조회할때
for i in range(10):
ls_rank = ls_parse.select('div.rank')[i].text
ls_itemname = ls_parse.select('div.info em a')[i].text
ls_dprice = ls_parse.select('div.info ul.sell span.sale')[i].text
#원래가격(c_price)이 없고 할인가격(d_price)만 있는 경우
# ls_cprice에는 공백을 담아두고 원래가격이 있으면 원래가격을 ls_cprice에 담음
#또한 j값을 -1시켜서 값이 밀려서 출력되지 않게함
ls_temp = ls_parse.select('div.info ul.sell')[i]
ls_temp2 = ls_temp.find(class_="c_price")
if ls_temp2 is None:
ls_cprice = ''
j=j-1
else:
ls_cprice = ls_parse.select('div.info ul.sell span.cost')[i+j].text
#select를 이용하여 이미지태그를 가져온후 그 속성에 src 부분의 이미지링크 가져오기
ls_imgtemp = ls_parse.select('div.info div.img img')[i]
#print(ls_imgtemp)
ls_img2 = ls_imgtemp.get('src')
print(ls_rank)
print(ls_itemname)
print(ls_cprice)
print(ls_dprice)
print(ls_img2)
temp_dict[str(ls_rank)] = {'itemname': ls_itemname, 'cprice': ls_cprice, 'dprice': ls_dprice, 'img': ls_img2}
#딕셔너리 자료형 출력
print(temp_dict)
댓글목록
등록된 댓글이 없습니다.