Python webscrape不能从所有容器中打印

问题描述 投票:0回答:1

我正在关注在线教程,以使用python抓取数据。但是我只在最后一个容器中得到结果吗?例如,该站点在12个容器下列出了12种不同的产品,而我的代码仅返回了最后一个容器中的数据,而不是全部12个。

非常感谢您的帮助。

谢谢亚历克斯

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.newegg.com/global/nz-en/Laptops-Notebooks/SubCategory/ID-32?Tid=1567456'

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()


page_soup = soup(page_html,"html.parser")
containers = page_soup.findAll("div",{"class":"item-container"})

for container in containers:

    brand_description = container.a.img["title"]
    price_box = container.findAll("li",{"class":"price-current"})
    price = price_box[0].strong.text

print("brand_description:" + brand_description)
print("price:" + price)
python web-scraping
1个回答
0
投票

您正在打印循环下面的值,因此仅在循环结束后才获得那些变量具有的值...所以是最后一个。在外观中移动打印件或将数据添加到列表中,然后在末尾打印整个列表。

for container in containers:

    brand_description = container.a.img["title"]
    price_box = container.findAll("li",{"class":"price-current"})
    price = price_box[0].strong.text

    print("brand_description:" + brand_description)
    print("price:" + price)

或替代:

brand_desc = []
prices = []
price_boxes = []

for container in containers:

    brand_desc.append(container.a.img["title"])
    price_boxes.append(container.findAll("li",{"class":"price-current"}))
    prices.append(price_box[0].strong.text)

print("brand_description:" + brand_desc)
print("price:" + prices)
© www.soinside.com 2019 - 2024. All rights reserved.