html - 使用python抓取ajax页面,但在请求几次后它返回假值
问题描述
我正在尝试使用 python 抓取 shopee 项目信息。
由于它使用 ajax,我正在尝试从以下位置提取商品信息:https ://shopee.com.my/api/v2/item/get?itemid=5859069631&shopid=206039726
经过几次请求,我发现它响应的 json 原来是假值(例如它的实际评分是 4.78 但它返回的是 0.24)。
我试图通过更改标头和 ip/proxy 来解决这个问题,但仍然无法正常工作。
有没有其他方法可以解决这个问题?
def get_info(url,itemurl):
requests.adapters.DEFAULT_RETRIES = 5
s = requests.session()
s.keep_alive = False
try:
fake_ua=UserAgent()
headers = {'User-Agent':fake_ua.random,
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'X-Shopee-Language': 'en',
'X-Requested-With': 'XMLHttpRequest',
'X-API-SOURCE': 'pc',
'If-None-Match-': '55b03-2ff39563c299cbdc937f8ab86ef322ab',
'DNT': '1',
'Referer': referer,
'TE': 'Trailers'}
ip = get_daili()
proxies = {"proxies":{"https":ip}}
response = requests.get(url, headers = headers, proxies = proxies, verify=False)
#response = requests.request("GET", url, headers=headers, data=payload)
if response.status_code == 200:
shop_info = response.json()
except requests.ConnectionError as e:
print(f' {url} error', e.args)
shop_name = shop_info['data']['name']
followers = shop_info['data']['follower_count']
ratinggood = shop_info['data']['rating_good']
ratingbad = shop_info['data']['rating_bad']
ratingnormal = shop_info['data']['rating_normal']
try:
fake_ua=UserAgent()
headers = {'User-Agent':fake_ua.random,
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'X-Shopee-Language': 'en',
'X-Requested-With': 'XMLHttpRequest',
'X-API-SOURCE': 'pc',
'If-None-Match-': '55b03-2ff39563c299cbdc937f8ab86ef322ab',
'DNT': '1',
'Referer': referer,
'TE': 'Trailers'}
ip = get_daili()
proxies = {"proxies":{"https":ip}}
response = requests.get(itemurl, headers = headers, proxies = proxies, verify=False)
#response = requests.request("GET", itemurl, headers=headers, data=payload)
if response.status_code == 200:
item_info = response.json()
except requests.ConnectionError as e:
print(f' {url} error', e.args)
#print(json.dumps(item_info, indent=4))
print(itemurl)
解决方案
我认为这是保护他们的 API 服务的算法,所以人们不能滥用他们的服务器。
也许您可以尝试使用 python selenium 和 Selenium Wire 来捕获数据。
推荐阅读
- c++ - 在 C++ 中,在返回时使用移动操作是什么意思?
- codeigniter - 使用codeigniter将特定ID传递给模型并从数据库中获取数据?
- java - Cassandra 查询无法解析获取数据
- docker - 如何在 Jenkins 声明式管道中的代理设置期间运行某些内容?
- plot - 箱线图,八度图
- java - 如何将 JtextArea 焦点设置在正确的位置?
- spring-boot - 功能区 MaxAutoRetries 属性不起作用
- python - 使用 Keras 连接隐藏单元
- php - 根据用户 id 检索数据 (CodeIgniter)
- php - 将 ZOHO API 与 Laravel 5.7 集成