I am using urllib.urlretrieve in python to download websites. Though some websites seem to not want me to download them, unless they have a proper referrer from their own site. Does anybody know of a way I can set a referrer in one of python's libraries or a external one to.
+1
A:
if you see the urllib2 docs here. there is example of how to put referrer to the header.
ghostdog74
2010-01-20 02:18:06
+1
A:
urllib
makes it hard to send arbitrary headers with the request; you could use urllib2, which lets you build and send a Request object with arbitrary headers (including of course the -- alas sadly spelled;-) -- Referer
). Doesn't offer urlretrieve
, but it's easy to just urlopen
as you with and copy the resulting file-like object to disk if you want (directly, or e.g. via shutil functions).
Alex Martelli
2010-01-20 02:19:05
+3
A:
import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
r = urllib2.urlopen(req)
adopted from http://docs.python.org/library/urllib2.html
Dyno Fu
2010-01-20 02:19:11