首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python请求返回200而不是301

问Python请求返回200而不是301
EN

Stack Overflow用户
提问于 2020-04-17 00:38:26
回答 1查看 1.1K关注 0票数 2

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

代码语言:javascript
复制
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永久移动

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

返回状态= 200 OK

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

我不明白为什么请求不会看到重定向。

有什么想法吗?

提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-17 00:54:42

默认情况下,Python模块在True中具有allow_redirect参数。我用False测试过它,它给出了你要找的301代码。

​

​

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

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

https://stackoverflow.com/questions/61262332

复制
相关文章

相似问题

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