Python请求返回200而不是301

时间:2020-04-17 00:38:26

标签: python python-requests

url =“ https://www.avito.ma/fr/2_mars/sacs_et_accessoires/Ch%C3%A2les_en_Vrai_Soie_Chanel_avec_boite_38445885.htm

try   
    r = requests.get(url,headers={'User-Agent': ua.random},timeout=timeout) # execute a timed website request
    if r.status_code > 299: # check for bad status
        r.raise_for_status() # if confirmed raise bad status
    else:
        print(r.status_code, url) # otherwise print status code and url
except Exception as e:
     print('\nThe following exception: {0}, \nhas been found found on the following post: "{1}".\n'.format(e,url))

预期状态 = 301永久移动

您可以访问页面或使用网址检查http://www.redirect-checker.org/index.php以获得正确的终端打印。

返回状态 = 200 OK

页面已移动,它应该返回上面的301 Moved Permanently,但是它返回200。我阅读了请求文档并检查了所有参数(allow_redirects = False等),但我不认为这是配置错误。

我很困惑为什么请求无法看到重定向。

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

Python请求模块默认情况下具有true的allow_redirect参数。我已经用False测试过了,它给出了您要查找的301代码。

enter image description here

在阅读了上面的注释后请注意:r.history将每个response_code保存在您当前的response_code之前,并保存在r.status_code中(仅当您将参数保留为True时)。