有没有一种简单的方法来请求Python中的网址,而不是进行重定向?有没有一种简单的方法来请求Pyth

2019-06-17 10:32发布

综观urllib2的来源,它看起来像这样做将是子类化HTTPRedirectHandler,然后用比如build_opener覆盖默认化HTTPRedirectHandler,最简单的方法,但是这似乎是很多的(相对复杂)工作,做什么似乎像它应该是很简单。

Answer 1:

下面是请求方式:

import requests
r = requests.get('http://github.com', allow_redirects=False)
print(r.status_code, r.headers['Location'])


Answer 2:

深入Python有处理一个很好用章重定向的urllib2。 另一种解决方案是httplib的 。

>>> import httplib
>>> conn = httplib.HTTPConnection("www.bogosoft.com")
>>> conn.request("GET", "")
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
301 Moved Permanently
>>> print r1.getheader('Location')
http://www.bogosoft.com/new/location


Answer 3:

这是一个不会跟随重定向一个urllib2的处理程序:

class NoRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        infourl = urllib.addinfourl(fp, headers, req.get_full_url())
        infourl.status = code
        infourl.code = code
        return infourl
    http_error_300 = http_error_302
    http_error_301 = http_error_302
    http_error_303 = http_error_302
    http_error_307 = http_error_302

opener = urllib2.build_opener(NoRedirectHandler())
urllib2.install_opener(opener)


Answer 4:

我想这将有助于

from httplib2 import Http
def get_html(uri,num_redirections=0): # put it as 0 for not to follow redirects
conn = Http()
return conn.request(uri,redirections=num_redirections)


Answer 5:

redirections在关键字httplib2请求方法是一种红色的鲱鱼。 而不是返回它将会引发的第一个请求RedirectLimit如果它收到一个重定向状态码异常事件。 要返回您需要设置inital响应follow_redirectsFalseHttp对象:

import httplib2
h = httplib2.Http()
h.follow_redirects = False
(response, body) = h.request("http://example.com")


Answer 6:

我第二OLT的指针, 深入Python 。 下面是使用的urllib2重定向处理程序的实现,比它更多的工作应该是什么? 也许,耸肩。

import sys
import urllib2

class RedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_301(self, req, fp, code, msg, headers):  
        result = urllib2.HTTPRedirectHandler.http_error_301( 
            self, req, fp, code, msg, headers)              
        result.status = code                                 
        raise Exception("Permanent Redirect: %s" % 301)

    def http_error_302(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_302(
            self, req, fp, code, msg, headers)              
        result.status = code                                
        raise Exception("Temporary Redirect: %s" % 302)

def main(script_name, url):
   opener = urllib2.build_opener(RedirectHandler)
   urllib2.install_opener(opener)
   print urllib2.urlopen(url).read()

if __name__ == "__main__":
    main(*sys.argv) 


Answer 7:

然而,最短的路

class NoRedirect(urllib2.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, hdrs, newurl):
        pass

noredir_opener = urllib2.build_opener(NoRedirect())


文章来源: Is there an easy way to request a URL in python and NOT follow redirects?