【发布时间】:2019-05-15 07:06:30
【问题描述】:
我正在尝试抓取代码和数据中列出的网站,我选择了特定的 div/类,但它似乎返回任何以“产品”开头的内容。你能帮我处理这段代码吗?感谢您对 Python 新手的帮助。另外,如果这可以写得更好,我很乐意接受。
url="https://shop.coles.com.au/a/a-tas-regional-glenorchy/everything/browse/dairy--eggs-meals/milk-3796059?pageNumber="
for x in range(1,6):
turl=url+str(x)
driver.get(turl)
time.sleep(10)
soup=BeautifulSoup(driver.page_source,"html.parser")
data = soup.find_all("div",{"class":"product"})
t=driver.find_elements_by_xpath('.//span[@class = "product-brand"]')
count=0
maxC=int(len(t))
for item in data:
if count < maxC and count>=0:
if item.find("span", {"class":"product-brand"}) is not None:
ProductText1=item.find("span", {"class":"product-brand"}).text
else:
ProductText1=""
if item.find("span", {"class":"product-name"}) is not None:
ProductText2=item.find("span", {"class":"product-name"}).text
else:
ProductText2=""
if item.find("span", {"class":"package-size"}) is not None:
size=item.find("span", {"class":"package-size"}).text
else:
size=""
if item.find("span", {"class":"package-price"}).text is not None:
Price=item.find("span", {"class":"package-price"}).text
else:
Price=""
if item.find("span", {"class":"product-qty"}).text is not None:
Price1=item.find("span", {"class":"product-qty"}).text
else:
Price1=""
if item.find("span", {"class":"product-price"}).text is not None:
Price2=item.find("span", {"class":"product-qty"}).text
else:
Price2=""
ProductText=str(ProductText1)+" "+str(ProductText2)
writer.writerow([ProductText,Price2,size,Price])
count+=1
【问题讨论】:
-
请更正您的缩进。当您复制粘贴代码时,它可能会丢失。这在 Python 中非常重要。
标签: python selenium web-scraping foreach beautifulsoup