page_soup.findall
似乎无法获取所有容器。当运行len(containers)时,它显示我有12个容器,但它只是从一个容器中提取信息。有人能帮忙吗。我正在尝试获取所有12个容器的信息
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.newegg.com/Video-Cards-Video-Devices/Category/ID-38?Tpk=graphics%20card'
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 = container.img["title"]
title_container = container.findAll("a",{"class":"item-title"})
product_name = title_container[0].text
shipping_container = container.findAll("li",{"class":"price-ship"})
shipping = shipping_container[0].text.strip()
print ("brand: " + brand)
print ("product_name: " + product_name)
print ("shipping : " + shipping)
您的代码看起来不错,它得到了所有12个容器,但您只打印了最后一个容器。 要打印全部,请使用循环内的最后三行打印行。像这样
相关问题 更多 >
编程相关推荐