我想要的只是刮掉所有产品。为什么我也不能使用containers.div?当我的教程只有<div><\div><div>
时,<div></div>
时我真的很困惑。
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://hbx.com/categories/sneakers'
# membuka koneksi, mengambil halaman
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
# html parsing
page_soup = soup(page_html, "html.parser")
# mengambil masing2 produk
containers = page_soup.findAll("div",{"class":"product-wrapper col-xs-6 col-sm-4"})
filename = "kontol.csv"
f = open(filename, "w")
headers = "judul, brand, harga\n"
f.write(headers)
for container in containers:
title_container = container.findAll("h3", {"class":"name"})
judul = title_container[0].text
brand_container = container.findAll("h4", {"class":"brand"})
brand = brand_container[0].text
price_container = container.findAll("span", {"class":"regular-price"})
harga = price_container[0].text
print("judul: " + judul)
print("brand: " + brand)
print("harga: " + harga)
f.write(judul + "," + brand + "," + harga + "\n")
f.close()
当我尝试使用container.findAll(&#34; h3&#34;,{&#34; class&#34;:&#34; name&#34;})时,我收到此错误
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()?
答案 0 :(得分:1)
尝试以下脚本并告诉我它没有解决问题。我使用条件语句来避免任何错误发生,如果任何项目都没有,就像第二个结果中的价格为无。现在它很棒。
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