网站始终使用python请求库挂起

时间:2018-07-30 01:50:22

标签: python html python-requests

我正在尝试使用python请求库从此网址https://www.adidas.com/api/products/EF2302/availability?sitePath=us中获取html

但是,每当我运行我的代码时,发出get请求时它就会挂起

header = BASE_REQUEST_HEADER
url = 'https://www.adidas.com/api/products/EF2302/availability?sitePath=us'
r = requests.get(url, headers = header)

我检查了chrome中的网络标签,并复制了包括用户代理在内的所有标头,所以这不是问题。我还能够在禁用了javascript和cookie的情况下以chrome加载页面。

此代码可与其他网站配合使用。我只是无法从任何阿迪达斯网站(包括https://www.adidas.com/us)中得到答复。

任何建议都将不胜感激。

2 个答案:

答案 0 :(得分:0)

一个不同的是User-agent字段,它要求将 用户代理:python-requests / 2.18.4

阿迪达斯可能只是丢弃了这些http请求,以阻止人们滥用其系统。

(顺便说一句,仅在www.adidas.com上也会发生这种情况)

我转载了这个问题,并看了一下wireshark数据包嗅探器。看来http请求很好,并且有tcp确认,但是没有http答复。

答案 1 :(得分:0)

该网站不喜欢请求提供的默认User-Agent字段,将其更改为Firefox / Chrome(我在示例中选择了Firefox),就可以成功读取数据:

from bs4 import BeautifulSoup
import requests
import json

headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0'}
url = 'https://www.adidas.com/api/products/EF2302/availability?sitePath=us'
r = requests.get(url, headers=headers)

json_data = json.loads(r.text)
print(json.dumps(json_data, indent=4))

打印:

{
    "id": "EF2302",
    "availability_status": "PREORDER",
    "variation_list": [
        {
            "sku": "EF2302_530",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "4",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_550",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "5",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_570",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "6",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_590",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "7",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_610",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "8",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_630",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "9",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_650",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "10",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_670",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "11",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_690",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "12",
            "instock_date": "2018-08-16T00:00:00.000Z"
        },
        {
            "sku": "EF2302_710",
            "availability": 15,
            "availability_status": "PREORDER",
            "size": "13",
            "instock_date": "2018-08-16T00:00:00.000Z"
        }
    ]
}