I need to get from Responce-Headers Location how to do it?

    2 answers 2

    Try this:

    from urllib.request import urlopen with urlopen('http://google.ru/') as rs: headers = dict(rs.getheaders()) print(headers) print(headers['Content-Type']) 
    • You can rs.headers (already as dict) directly instead of getheaders() (a list of pairs). Although the Location http header probably will not get, because urllib.request.urlopen() follows 30x redirections automatically. Another question header contains urllib3 , not urllib although this may be a simple typo. - jfs
    • hmm, it seems, when writing the answer there was just urllib ... - gil9red
    • the question has no edits and your answer is published after the end of the five-minute grace period (when you can edit without a trace). But this is not important, since you probably guessed that the author probably wanted urllib , not urllib3 . - jfs

    Response-Headers are available as response.headers in Python:

     #!/usr/bin/env python3 from urllib.request import urlopen with urlopen(url) as response: print(response.headers['Content-Length']) print(response.get_content_charset('not found')) # character encoding 

    urlopen() automatically follows 30x redirects, so you probably won't see response.headers['Location'] . You can turn off redirection or use http.client directly ( urlopen() uses http.client inside) to get the Location http header from the server response to the HEAD http request:

     #!/usr/bin/env python3 import http.client conn = http.client.HTTPConnection('httpbin.org') conn.request('HEAD', '/redirect/2') response = conn.getresponse() print(response.status) # -> 302 print(response.headers['Location']) # -> /relative-redirect/1 

    If you want to get the headers in urllib3 (not from the standard library), then a slightly different usage example, but you can still use the headers attribute to get the http headers from the response:

     import urllib3 # $ pip install urllib3 http = urllib3.PoolManager() r = http.request('GET', 'http://httpbin.org/redirect/2', redirect=False) print(r.status) # -> 302 print(r.headers['Location']) # -> /relative-redirect/1 

    Probably, instead of urllib3 , you want to use the requests library to use, which provides a higher level interface ( requests uses urllib3 inside) and is the recommended http client for Python:

     >>> import requests # $ pip install requests >>> r = requests.head('http://httpbin.org/redirect/2') >>> r.status_code 302 >>> r.headers['Location'] '/relative-redirect/1' >>> r.history [] >>> r = requests.get('http://httpbin.org/redirect/2') >>> r.status_code 200 >>> r.headers['Location'] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/.../requests/structures.py", line 56, in __getitem__ return self._store[key.lower()][1] KeyError: 'location' >>> r.history [<Response [302]>, <Response [302]>] 

    To try the library without installing it, you can use the version that comes with pip : from pip._vendor import requests .