简体   繁体   English

当服务器使用 urllib2.urlopen(url).geturl() 重定向我时,我无法获取整个 url

[英]I can not get the whole url when the server redirect me by using urllib2.urlopen(url).geturl()

例如,我只能得到'http://www.stackoverflow.com'如果整个URL是'http://www.stackoverflow.com?key=value&key1=value1'

urllib2 does not strip the query string after a redirect: urllib2在重定向后不会剥离查询字符串:

>>> import urllib2
>>> r = urllib2.urlopen('http://httpbin.org/redirect-to?url=http://example.com/%3Ffoo=bar')
>>> r.geturl()
'http://example.com/?foo=bar'

Perhaps you are using a website that redirects you again on requests with a query string?也许您正在使用一个网站,该网站会根据带有查询字符串的请求再次重定向您?

You could use the requests library instead;您可以改用requests you can either disable redirects altogether, or you can introspect the history of redirections:您可以完全禁用重定向,也可以自省重定向的历史记录:

>>> import requests 
>>> r = requests.get('http://httpbin.org/relative-redirect/4')
>>> r.history
[<Response [302]>, <Response [302]>, <Response [302]>, <Response [302]>]
>>> r.history[2].url
u'http://httpbin.org/relative-redirect/2'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM