【发布时间】:2019-03-24 16:59:08
【问题描述】:
我正在编写一个脚本来生成 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 类的任何内容,但这没有任何意义。
【问题讨论】:
标签: python python-3.x beautifulsoup attributeerror