프로그래밍에 익숙하지 않고 파이썬으로 처음으로 작은 웹 크롤러를 만들려고합니다.WebCrawler, 할인 된 가격의 항목이 일부 있음 - 색인 오류
목표 :는 제품 목록 페이지를 크롤링 -
상태 파일을 CSV로 저장 - 브랜드 이름, 문서 이름, 원래 가격과 새로운 가격 스크 레이 핑 : 나는 브랜드 이름을 얻기 위해 관리했습니다, 기사를 이름뿐 아니라 원래 가격을 입력하고 올바른 순서대로 목록에 넣으십시오 (예 : 10 개 제품). 모든 항목에 대해 브랜드 이름, 설명 및 가격이 있으므로 내 코드는 올바른 순서로 CSV에 가져옵니다.
코드 :
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
myUrl = 'https://www.zalando.de/rucksaecke-herren/'
#open connection, grabbing page, saving in page_html and closing connection
uClient = uReq(myUrl)
page_html = uClient.read()
uClient.close()
#Datatype, html paser
page_soup = soup(page_html, "html.parser")
#grabbing information
brand_Names = page_soup.findAll("div",{"class": "z-nvg-cognac_brandName-2XZRz z-nvg-cognac_textFormat-16QFn"})
articale_Names = page_soup.findAll ("div",{"class": "z-nvg-cognac_articleName--arFp z-nvg-cognac_textFormat-16QFn"})
original_Prices = page_soup.findAll("div",{"class": "z-nvg-cognac_originalPrice-2Oy4G"})
new_Prices = page_soup.findAll("div",{"class": "z-nvg-cognac_promotionalPrice-3GRE7"})
#opening a csv file and printing its header
filename = "XXX.csv"
file = open(filename, "w")
headers = "BRAND, ARTICALE NAME, OLD PRICE, NEW PRICE\n"
file.write(headers)
#How many brands on page?
products_on_page = len(brand_Names)
#Looping through all brands, atricles, prices and writing the text into the CSV
for i in range(products_on_page):
brand = brand_Names[i].text
articale_Name = articale_Names[i].text
price = original_Prices[i].text
new_Price = new_Prices[i].text
file.write(brand + "," + articale_Name + "," + price.replace(",",".") + new_Price.replace(",",".") +"\n")
#closing CSV
file.close()
문제 : 적절한 장소에 내 CSV로 할인 된 가격을 받고 고민하고. 아니 모든 항목이 할인을 가지고 있으며, 나는 현재 내 코드에 두 가지 문제 참조 : 나는 웹 사이트에있는 정보를 찾기 위해 .findAll를 사용
을 - 다음 전체 제품 덜 할인 된 제품이 있기 때문에, 내 new_Prices 적은 포함 가격 (예 : 10 개 제품의 경우 3 개 가격). 만약 내가 목록에 추가 할 수있을 것이라고, 나는 그들이 처음 3 행에 나타납니다 가정합니다. 올바른 prodcuts에 new_Prices를 추가하려면 어떻게해야합니까?
"색인 오류 : 목록 색인 범위를 벗어남"오류가 발생합니다. 오류는 10 개 제품을 통해 반복되고 있지만 new_Prices는 다른 목록보다 빨리 끝납니다. ? 그게 말이 되겠습니까? 내 가정이 맞습니까?
나는 많은 도움을 주심.
감사,
토르스텐
코드의 스크린 샷을 게시하지 마시고 관련 코드를 코드 블록에 복사하십시오. – bgse
입력 예제도 게시하십시오 – Guilherme
@bgse 코드로 블록으로 업데이트 –