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

时间:2019-11-09 23:43:55

标签: python web-scraping

我正在关注在线教程,以使用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)

1 个答案:

答案 0 :(得分: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)