用Python实现亚马逊Amazon高性能爬虫采集销量信息
import requests
from bs4 import BeautifulSoup
import pandas as pd
# 设置用户代理,以模拟真实的浏览器访问
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 获取ASIN对应的销量信息
def get_product_ranking(asin):
url = f'https://www.amazon.com/dp/{asin}/ref=cm_cr_arp_d_paging_btm_2?ie=UTF8'
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# 解析销量信息
ranking_data = soup.find('li', {'id': 'SalesRank'})
if ranking_data:
ranking = ranking_data.text.strip()
else:
ranking = 'N/A'
return ranking
except requests.RequestException as e:
print(f'Error: {e}')
return 'Error'
# 示例:获取ASIN为B01M8YGI1Y的产品销量信息
ranking = get_product_ranking('B01M8YGI1Y')
print(ranking)
这段代码首先导入了必要的模块,设置了请求头以模拟浏览器访问,定义了一个获取产品销量信息的函数,并尝试获取了特定ASIN对应的销量信息。最后,它打印出了获取的销量信息或在发生错误时打印错误信息。这个例子展示了如何使用Python高效地抓取亚马逊的具体产品信息。
评论已关闭