Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a header to urllib2 opener?

cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))     opener.open('http://abc.com') opener.open('http://google.com') 

As you can see, I use opener to visit different websites, using a cookie jar. Can I set a header so that each time a website is it, the header is applied?

like image 296
TIMEX Avatar asked Jun 07 '11 00:06

TIMEX


People also ask

What is the difference between Urllib and urllib3?

The Python 3 standard library has a new urllib which is a merged/refactored/rewritten version of the older modules. urllib3 is a third-party package (i.e., not in CPython's standard library).

What does urllib2 do in Python?

urllib2 is a Python module that can be used for fetching URLs. The magic starts with importing the urllib2 module.


2 Answers

You can add the headers directly to the OpenerDirector object returned by build_opener. From the last example in the urllib2 docs:

OpenerDirector automatically adds a User-Agent header to every Request. To change this:

import urllib2 opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] opener.open('http://www.example.com/') 

Also, remember that a few standard headers (Content-Length, Content-Type and Host) are added when the Request is passed to urlopen() (or OpenerDirector.open()).

like image 87
senderle Avatar answered Sep 20 '22 15:09

senderle


headers = {'foo': 'bar',} req = urllib2.Request(url, None, headers) resp = urllib2.urlopen(req) 

or

req = urllib2.Request(url) req.add_header('foo', 'bar') resp = urllib2.urlopen(req) 
like image 20
Corey Goldberg Avatar answered Sep 16 '22 15:09

Corey Goldberg