我正在编写一个脚本来生成 JSON 文件,但我遇到了一些问题。
import requests
from bs4 import BeautifulSoup
url = requests.get('https://www.perfectimprints.com/custom-promos/20492/Beach-Balls.html')
source = BeautifulSoup(url.text, 'html.parser')
product_feed = source.find('div', id_="pageBody")
products = product_feed.find_all('div', class_="product_wrapper")
single_product = products[0]
product_name = single_product.find('div', class_="product_name")
product_name = product_name.a.text
sku = single_product.find('div', class_="product_sku")
sku = sku.text
def get_product_details(product):
product_name = product.find('div', class_="product_name").a.text
sku = single_product.find('div', class_="product_sku").text
return {
"product_name": product_name,
"sku": sku
}
all_products = [get_product_details(product) for product in products]
print(all_products)
我得到的错误消息是:Traceback (most recent call last):
File "scrape.py", line 9, in <module>
products = product_feed.find_all('div', class_="product_wrapper")
AttributeError: 'NoneType' object has no attribute 'find_all'
从我的阅读来看,这是因为它在product_wrapper
类中找不到任何东西,但这没有任何意义。
问题是product_feed = source.find('h1', id_="pageBody")
正在返回None
。我尝试了您的代码,product_feed = source.find_all('h1')
只返回 1 个没有 id 信息的项目。
你不需要product_feed
,删除它并将下一行更改为:
products = source.find_all('div', class_="product_wrapper")
最后可以验证:print(len(all_products))
48
查看网站的源代码,id="pageBody" 的元素是一个div
,而不是h1
。因此,当您使用source.find
时,它会返回None
。尝试:
...
product_feed = source.find('div', id_="pageBody")
...