开发者

how do python capture 302 redirect url

i try to fetch data from web,but the page use a 302 redirect how can i use python to fetch the real ur开发者_如何转开发l?


Have a look at chapter 11.7. Handling redirects from the Dive Into Python series. It explains your entire issue in quite a bit of detail, example code and all.


What are you currently using? Both urllib and urllib2 should handle it automatically:

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']


If you are using the http.client.HTTPConnection(3.x), or httplib.HTTPConnection(2.x), just obtain the Location Header:

response.getheader('Location')

I know this works at least on craigslist.org

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜