Python веб-парсинг - не отображаются все контейнеры - PullRequest
0 голосов
/ 20 июня 2020

page_soup.findall похоже не получит все контейнеры. при запуске len (container) он показывает, что у меня 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)

1 Ответ

1 голос
/ 20 июня 2020

ваш код выглядит хорошо, он получает все 12 контейнеров, но вы печатаете только последний. чтобы напечатать все, используйте последние три строки печати внутри для l oop. вот так

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)
...