简体   繁体   English

Python 请求返回 200 而不是 301

[英]Python requests returns 200 instead of 301

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

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))

Expected status = 301 Moved Permanently预期状态= 301 永久移动

You can visit the page or check http://www.redirect-checker.org/index.php with the url for a correct terminal print.您可以访问该页面或检查http://www.redirect-checker.org/index.php和 url 以获得正确的终端打印。

Returned status = 200 OK返回状态= 200 OK

The page has been moved and it should return the above 301 Moved Permanently , however it returns a 200. I read the requests doc and checked all the parameters (allow_redirects=False etc.) but I don't think it is a mistake of configuration.该页面已被移动,它应该返回上述301 Moved Permanently ,但它返回 200。我阅读了请求文档并检查了所有参数(allow_redirects=False 等),但我认为这不是配置错误.

I am puzzled at why requests wouldn't see the redirects.我对为什么请求看不到重定向感到困惑。

Any ideas?有任何想法吗?

Thank you in advance.先感谢您。

Python Requests module has the allow_redirect parameter in True by default. Python Requests 模块默认将 allow_redirect 参数设置为 True。 I've tested it with False and it gives the 301 code that you're looking for.我已经用 False 对其进行了测试,它给出了您正在寻找的 301 代码。

在此处输入图像描述

Note after reading your comment above: r.history saves each response_code before the one that you're right now which is saved in r.status_code (only if you leave the parameter in True).阅读上述评论后请注意:r.history 将每个 response_code 保存在您现在保存在 r.status_code 中的每个 response_code 之前(仅当您将参数保留为 True 时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python请求标头显示“ 301”,而获取返回“ 200” - Python requests head shows “301” while get returns “200” Python Flask Web服务器未收到JSON Ajax POST请求,服务器返回HTTP 200 OPTIONS而不是201 POST - Python Flask webserver not receiving JSON ajax POST requests, server returns HTTP 200 OPTIONS instead of 201 POST Python requests.post() 方法命令返回 200 status_code 属性而不是 500 - Python requests.post() method command returns 200 status_code attribute instead of 500 Github API从python请求中返回401,但从curl返回200 - Github API returns 401 from python requests but 200 from curl 我的python请求返回401而邮递员返回200 - my python requests return 401 while postman returns 200 得到<response[200]>使用 Python http 请求而不是 INT - Getting <response[200]> with Python http requests instead of INT 请求给出 404 而不是 200 - Requests gives 404 instead of 200 Django API 测试:预期状态代码 200,而不是收到 301 - Django API test: expected status code 200, instead received 301 request.get()返回200,但主体为空 - requests.get() returns 200 but body is empty 为什么在我的帖子请求中得到返回码 200 而不是 201? (Python/请求/Json) - Why did I get return code 200 instead of 201 on my post request ? (Python /Requests/Json)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM