Python 为什么我能';t调用container.findAll(“";h3";,{";class";:";name";})?

Python 为什么我能';t调用container.findAll(“";h3";,{";class";:";name";})?,python,html,python-3.x,web-scraping,beautifulsoup,Python,Html,Python 3.x,Web Scraping,Beautifulsoup,我要的是把所有的产品都刮干净。为什么我也不能使用containers.div?我真的很困惑,当有时,我的教程只是 当我尝试使用container.findAll(“h3”,{“class”:“name”})调用时,我遇到了这个错误 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python36\lib\site-packages\bs4\element

我要的是把所有的产品都刮干净。为什么我也不能使用containers.div?我真的很困惑,当有
时,我的教程只是

当我尝试使用container.findAll(“h3”,{“class”:“name”})调用时,我遇到了这个错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36\lib\site-packages\bs4\element.py", line 1807, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'findAll'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python36\lib\site packages\bs4\element.py”,第1807行,在\uuu getattr中__
“ResultSet对象没有属性“%s”。您可能将项目列表视为单个项目。当您打算调用find()时是否调用find_all()?%key?”
AttributeError:ResultSet对象没有属性“findAll”。您可能将项目列表视为单个项目。当您打算调用find()时,是否调用了find_all()?

试试下面的脚本,告诉我它没有解决问题。我使用了条件语句来避免在任何项为none时发生的任何错误,如在第二个结果中price为none。现在它工作得很好

import requests ; from bs4 import BeautifulSoup

url = "https://hbx.com/categories/sneakers"
soup = BeautifulSoup(requests.get(url).text,"lxml")
for item in soup.find_all(class_="product-box"):
    name = item.find(class_="name").text if item.find(class_="name") else ""
    brand = item.find(class_="brand").text if item.find(class_="brand") else ""
    price = item.find(class_="regular-price").text if item.find(class_="regular-price") else ""
    print(name,brand,price)
或者使用
find_all
,如果您愿意的话。然而,结果总是一样的

for item in soup.find_all(class_="product-box"):
    name = item.find_all(class_="name")[0].text if item.find_all(class_="name") else ""
    brand = item.find_all(class_="brand")[0].text if item.find_all(class_="brand") else ""
    price = item.find_all(class_="regular-price")[0].text if item.find_all(class_="regular-price") else ""
    print(name,brand,price)
部分结果:

Club C 85 Reebok USD 75.00
NMD R2 Runner Primeknit Adidas Originals 
NMD R2 Runner Adidas Originals USD 155.00

在我自己的计算机上运行此代码之后,似乎您将遇到一些问题,即使用urllib从该站点中删除此数据。似乎大部分内容都是使用javascript呈现的,这将使您无法使用urllib对其进行刮取。我建议研究使用硒来解决这个问题:。
Club C 85 Reebok USD 75.00
NMD R2 Runner Primeknit Adidas Originals 
NMD R2 Runner Adidas Originals USD 155.00