【发布时间】:2020-07-30 09:45:01
【问题描述】:
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 等),但我不认为这是配置错误。
我对为什么请求看不到重定向感到困惑。
有什么想法吗?
提前谢谢你。
【问题讨论】:
-
在您提出请求后 -
r.url和r.history会向您展示什么? -
r.url ==
200 https://www.avito.ma/fr/2_mars/sacs_et_accessoires/Ch%C3%A2les_en_Vrai_Soie_Chanel_avec_boite_38445885.htm, r.historyThe following exception: 503 Server Error: Service Unavailable: Back-end server is at capacity for url: https://www.avito.ma/fr/2_mars/sacs_et_accessoires/Ch%C3%A2les_en_Vrai_Soie_Chanel_avec_boite_38445885.htm, has been found found on the following post: "https://www.avito.ma/fr/2_mars/sacs_et_accessoires/Ch%C3%A2les_en_Vrai_Soie_Chanel_avec_boite_38445885.htm". --Return--. -
会不会是服务器配置的问题?
-
我在
ipdb中重新创建了 r.history,并将结果粘贴到了上面。 -
从代码本身打印
r.history会返回:[<Response [301]>, <Response [301]>]这应该是正确的状态代码。但我仍然无法理解history和status_code有何不同?