【发布时间】:2011-04-22 16:52:12
【问题描述】:
我尝试从网络获取数据,但页面使用 302 重定向 如何使用 python 获取真实的 url?
【问题讨论】:
标签: python url redirect capture
我尝试从网络获取数据,但页面使用 302 重定向 如何使用 python 获取真实的 url?
【问题讨论】:
标签: python url redirect capture
查看Dive Into Python 系列中的chapter 11.7. Handling redirects。它非常详细地解释了您的整个问题、示例代码和所有内容。
【讨论】:
您目前正在使用什么? urllib 和 urllib2 都应该自动处理它:
page = urllib.urlopen('http://mrozekma.com/302test.php')
>>> print page.geturl() # This will show the redirected-to URL
http://mrozekma.com/302test.php?success
>>> print page.readlines()
['Success']
【讨论】:
如果您使用的是 http.client.HTTPConnection(3.x) 或 httplib.HTTPConnection(2.x),只需获取 Location Header:
response.getheader('Location')
我知道这至少在 craigslist.org 上有效
【讨论】: