尝试从亚马逊评论中获取产品标题 Url

Trying to get product title from Amazon Review Url

我使用的url是:https://www.amazon.com/Marmot-womens-Precip-Lightweight-Waterproof/product-reviews/B086YML34N/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews

product_name = soup.findall("h1",{"class":"a-size-large a-text-ellipsis"})

我正在尝试获取页面上所有评论的产品标题。我尝试使用上面的代码,但出现“nonetype”错误。我代码中的其余属性确实有效。我在想我没有选对 class。有没有人有从亚马逊抓取的经验,可以就我做错了什么提供一些建议?

您可能有验证码页面。尝试指定不同的 HTTP headers(在我的例子中是 User-AgentAccept-Language):

import requests 
from bs4 import BeautifulSoup


url = 'https://www.amazon.com/Marmot-womens-Precip-Lightweight-Waterproof/product-reviews/B086YML34N/ref=cm_cr_dp_d_show_all_btm?ie=UTF8&reviewerType=all_reviews'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0',
'Accept-Language': 'en-US,en;q=0.5'}

soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')
print(soup.select_one('h1.a-size-large.a-text-ellipsis').text)

打印:

Marmot womens Precip Lightweight Waterproof Rain Jacket

因为 bs4 中没有 findall。它是 find_allfindAll(大写 A)。