Python Requests库重定向新url

Dan*_*lch 84 python redirect http python-requests

我一直在浏览Python Requests文档,但是我看不到任何我想要实现的功能.

在我的脚本中,我正在设置allow_redirects=True.

我想知道页面是否已被重定向到其他内容,新URL是什么.

例如,如果起始URL是: www.google.com/redirect

最后的URL是 www.google.co.uk/redirected

我如何获得该URL?

Mar*_*ers 130

您正在寻找请求历史记录.

response.history属性是导致最终URL的响应列表,可以在其中找到response.url.

response = requests.get(someurl)
if response.history:
    print "Request was redirected"
    for resp in response.history:
        print resp.status_code, resp.url
    print "Final destination:"
    print response.status_code, response.url
else:
    print "Request was not redirected"
Run Code Online (Sandbox Code Playgroud)

演示:

>>> import requests
>>> response = requests.get('http://httpbin.org/redirect/3')
>>> response.history
(<Response [302]>, <Response [302]>, <Response [302]>)
>>> for resp in response.history:
...     print resp.status_code, resp.url
... 
302 http://httpbin.org/redirect/3
302 http://httpbin.org/redirect/2
302 http://httpbin.org/redirect/1
>>> print response.status_code, response.url
200 http://httpbin.org/get
Run Code Online (Sandbox Code Playgroud)

  • httpbin.org 由于某种原因给出 404,但 httpbingo.org (相同的 URL 方案)对我来说工作得很好。 (2认同)
  • @PrestonBadeer:这是一个已知问题:https://github.com/postmanlabs/httpbin/issues/617。幸运的是,演示是否能找到答案并不重要。 (2认同)

hwj*_*wjp 50

这回答了一个稍微不同的问题,但由于我自己也陷入了困境,我希望它对其他人有用.

如果您想使用allow_redirects=False并直接获取第一个重定向对象,而不是跟随它们的链,并且您只想直接从302响应对象获取重定向位置,那么r.url将无法工作.相反,它是"位置"标题:

r = requests.get('http://github.com/', allow_redirects=False)
r.status_code  # 302
r.url  # http://github.com, not https.
r.headers['Location']  # https://github.com/ -- the redirect destination
Run Code Online (Sandbox Code Playgroud)


Bac*_*ics 28

文档有这个模糊http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history

import requests

r = requests.get('http://www.github.com')
r.url
#returns https://www.github.com instead of the http page you asked for 
Run Code Online (Sandbox Code Playgroud)


Gen*_*wen 25

我认为request.head而不是requests.get在处理url重定向时会更安全,请在这里查看github问题:

r = requests.head(url, allow_redirects=True)
print(r.url)
Run Code Online (Sandbox Code Playgroud)

  • @Volatil3:并非所有服务器都以与GET相同的方式响应HEAD请求. (5认同)
  • 这应该是公认的答案。简短而甜蜜。 (2认同)

小智 8

对于python3.5,您可以使用以下代码:

import urllib.request
res = urllib.request.urlopen(starturl)
finalurl = res.geturl()
print(finalurl)
Run Code Online (Sandbox Code Playgroud)