当前位置: 首页 > news >正文

亚马逊商品评论爬取与情感分析:Python+BeautifulSoup实战(含防封策略)

一、数据爬取模块(Python示例)

import requests
from bs4 import BeautifulSoup
import pandas as pd
import timeheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36','Accept-Language': 'en-US'
}def scrape_amazon_reviews(product_id, max_pages=5):base_url = f"https://www.amazon.com/product-reviews/{product_id}"reviews = []for page in range(1, max_pages + 1):url = f"{base_url}/?pageNumber={page}"response = requests.get(url, headers=headers)soup = BeautifulSoup(response.text, 'html.parser')for review in soup.find_all('div', {'data-hook': 'review'}):review_data = {'rating': float(review.find('i', {'data-hook': 'review-star-rating'}).text.split()[0]),'title': review.find('a', {'data-hook': 'review-title'}).text.strip(),'body': review.find('span', {'data-hook': 'review-body'}).text.strip(),'date': review.find('span', {'data-hook': 'review-date'}).text}reviews.append(review_data)time.sleep(2)  # 降低请求频率return pd.DataFrame(reviews)

关键点说明

  • 需替换product_id为目标商品ASIN码

  • 通过time.sleep()规避反爬机制

  • 使用data-hook属性精准定位评论元素


二、情感分析模块(NLP示例)

from textblob import TextBlobdef analyze_sentiment(review_text):analysis = TextBlob(review_text)return {'polarity': analysis.sentiment.polarity,  # 情感极性(-1到1)'subjectivity': analysis.sentiment.subjectivity  # 主观性(0到1)}

输出应用

  • 极性>0.3判定为积极评论

  • 极性<-0.3判定为消极评论


三、数据可视化(Matplotlib示例)

import matplotlib.pyplot as pltdef plot_rating_distribution(df):plt.figure(figsize=(8, 4))df['rating'].value_counts().sort_index().plot(kind='bar', color='#FF9900')plt.title('Amazon Review Rating Distribution')plt.xlabel('Star Rating')plt.ylabel('Count')plt.xticks(rotation=0)plt.show()

四、合规性注意事项

  1. 遵守亚马逊Robots协议(检查/robots.txt

  2. 单IP请求频率建议≤2次/秒

  3. 商业用途需申请官方API(MWS或SP-API)

相关文章:

  • 智绅科技——科技赋能健康养老,构建智慧晚年新生态
  • SpringAI系列 - 升级1.0.0
  • SpringBatch+Mysql+hanlp简版智能搜索
  • android协程异步编程常用方法
  • MySQL之数据库的内嵌函数和联合查询
  • 广东省林学会新办林业造林资质具体条件?
  • 1-1 初探Dart编程语言
  • Augment vs Cursor:当Cursor解决不了问题时的最佳补充方案
  • AI+爆款文案,提示词脚本 ——卫朋
  • Linux(9)——进程(控制篇——下)
  • 电磁器件的“折纸革命“:牛津《Sci. Reports》发布剪纸超材料
  • gitlib 常见命令
  • C++写入CSV的操作读取、写入、追加以及文件删除
  • Unsafe.putOrderedInt与Volatile
  • 软件需求文档如何做精确度度量
  • 告别集成泥潭,拥抱松耦合、高弹性的现代化应用-Amazon EventBridge
  • 需求可测试性评价
  • Tomcat JK2 连接器安装教程:jakarta-tomcat-connectors-jk2-src-current.tar.gz 配置步骤详解
  • 第2章-12 输出三角形面积和周长(走弯路解法)
  • yolov8添加注意力机制
  • 镇江网站建设/免费推广平台排行
  • 郑州的网站建设公司有哪些/交换链接的方法
  • 南通市建设委员会网站/seo相关岗位
  • 个人资料库网站怎么做/宁波做网站的公司
  • 外贸网站seo招聘/合肥网站seo整站优化
  • 网站自己怎么做优化/数据分析软件