Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8629

Re: urllib, urlretrieve method, how to get headers?

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: urllib, urlretrieve method, how to get headers?
Followup-To comp.lang.python
Date 2011-07-01 09:43 +0200
Organization None
Message-ID <iujtq4$srp$1@solani.org> (permalink)
References <mailman.536.1309503827.1164.python-list@python.org>

Followups directed to: comp.lang.python

Show all headers | View raw


Даниил Рыжков wrote:

> How can I get headers with urlretrieve? I want to send request and get
> headers with necessary information before I execute urlretrieve(). Or
> are there any alternatives for urlretrieve()?
 
It's easy to do it manually:

>>> import urllib2

Connect to website and inspect headers:

>>> f = urllib2.urlopen("http://www.python.org")
>>> f.headers["Content-Type"]
'text/html'

Write page content to file:

>>> with open("tmp.html", "w") as dest:
...     dest.writelines(f)
...

Did we get what we expected?

>>> with open("tmp.html") as f: print f.read().split("title")[1]
...
>Python Programming Language &ndash; Official Website</
>>>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

urllib, urlretrieve method, how to get headers? Даниил Рыжков <daniil.re@gmail.com> - 2011-07-01 18:03 +1100
  Re: urllib, urlretrieve method, how to get headers? Peter Otten <__peter__@web.de> - 2011-07-01 09:43 +0200
    Re: urllib, urlretrieve method, how to get headers? Даниил Рыжков <daniil.re@gmail.com> - 2011-07-01 19:26 +1100
    Re: urllib, urlretrieve method, how to get headers? Даниил Рыжков <daniil.re@gmail.com> - 2011-07-01 19:53 +1100
    Re: urllib, urlretrieve method, how to get headers? Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2011-07-01 22:31 +0530
    Re: urllib, urlretrieve method, how to get headers? Chris Rebert <clp2@rebertia.com> - 2011-07-01 10:02 -0700
    Re: urllib, urlretrieve method, how to get headers? Даниил Рыжков <daniil.re@gmail.com> - 2011-07-02 11:47 +1100

csiph-web