如何为urllib2开头添加标题?

时间:2022-12-07 18:09:57
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?

如您所见,我使用开页器访问不同的网站,使用一个cookie jar。我是否可以设置一个标题,以便每次网站出现时,标题都被应用?

2 个解决方案

#1


60  

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

可以直接将header添加到build_opener返回的OpenerDirector对象中。来自urllib2文档的最后一个例子:

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

OpenerDirector自动向每个请求添加用户代理头部。改变:

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()).

另外,请记住,当请求被传递给urlopen()(或OpenerDirector.open())时,会添加一些标准的标头(内容长度、内容类型和主机)。

#2


17  

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)

#1


60  

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

可以直接将header添加到build_opener返回的OpenerDirector对象中。来自urllib2文档的最后一个例子:

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

OpenerDirector自动向每个请求添加用户代理头部。改变:

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()).

另外,请记住,当请求被传递给urlopen()(或OpenerDirector.open())时,会添加一些标准的标头(内容长度、内容类型和主机)。

#2


17  

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)