python webscrape不显示所有容器

2024-05-15 05:00:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

Tags: nametitlecontainerhtmlpageproduct容器class
1条回答
网友
1楼 · 发布于 2024-05-15 05:00:29

您的代码看起来不错,它得到了所有12个容器,但您只打印了最后一个容器。 要打印全部,请使用循环内的最后三行打印行。像这样

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)

相关问题 更多 >

    热门问题