[英]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.