嗯,老实说,我不知道你使用的一些路径在哪里,因为我找不到它们。我已重做了您的代码以尝试帮助:
from lxml import html
import requests
import json
asin = 'B0718Y23CQ'
page_response = requests.get('https://www.amazon.com/product-reviews/'+ asin)
parser = html.fromstring(page_response.content)
reviews_html = parser.xpath('//div[@class="a-section review"]')
reviews_arr = []
for review in reviews_html:
review_dic = {}
review_dic['title'] = review.xpath('.//a[@data-hook="review-title"]/text()')
review_dic['rating'] = review.xpath('.//a[@class="a-link-normal"]/@title')
review_dic['author'] = review.xpath('.//a[@data-hook="review-author"]/text()')
review_dic['date'] = review.xpath('.//span[@data-hook="review-date"]/text()')
review_dic['purchase'] = review.xpath('.//span[@data-hook="avp-badge"]/text()')
review_dic['review_text'] = review.xpath('.//span[@data-hook="review-body"]/text()')
review_dic['helpful_votes'] = review.xpath('.//span[@data-hook="helpful-vote-statement"]/text()')
reviews_arr.append(review_dic)
print(json.dumps(reviews_arr, indent = 4))
输出方案为:
{
"title": [
"I find it very useful, I use for anything I need"
],
"rating": [
"5.0 out of 5 stars"
],
"author": [
"Nicoletta Delon"
],
"date": [
"on January 2, 2018"
],
"purchase": [
"Verified Purchase"
],
"review_text": [
"I like this a lot. I use it a lot. It's a medium to small size but it holds a lot."
],
"helpful_votes": [
"\n One person found this helpful.\n "
]
}
现在,您必须清理结果,将其从列表中删除,防止元素为空,我认为您将获得所需的内容。
要获得所有评论,您必须迭代页面,添加
?pageNumber=1
并迭代该数字。您可以使用代理来防止ip阻塞,以防您发出许多请求。