首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用python抓取ajax页面,但在多次请求之后,它将返回假值

使用python抓取ajax页面,但在多次请求之后,它将返回假值
EN

Stack Overflow用户
提问于 2021-06-09 00:44:29
回答 1查看 39关注 0票数 2

我正在尝试使用python抓取购物者的商品信息。

https://shopee.com.my/All%20in%20one%20pc%20Intel%20core%20I3/I5/I7%20Dual-core%208G%20RAM%20128%20gb%20SSD%20With%20optical%20drive%20CD%2023.8%20Inch%20computer%20Office%20Desktop%20All-in-one%20desktop%20Support%20WiFi-i.206039726.5859069631为例。

因为它使用ajax,所以我尝试从以下位置提取项目信息:https://shopee.com.my/api/v2/item/get?itemid=5859069631&shopid=206039726

经过几次请求,我发现它响应的json原来是假值(比如它的实际评级是4.78,但它返回0.24)。

我试图通过更改报头和ip/代理来解决这个问题,但仍然不起作用。

有没有其他方法可以潜在地解决这个问题?

代码语言:javascript
运行
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-06-20 23:30:55

我认为是算法来保护他们的API服务,这样人们就不会滥用他们的服务器。

也许您可以尝试使用python selenium和Selenium Wire来捕获数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67891213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档