如何在HTTP请求中使用urllib2发送自定义标头?

时间:2022-12-07 18:09:45

I want to send a custom "Accept" header in my request when using urllib2.urlopen(..). How do I do that?

我想在使用urllib2.urlopen(..)时在我的请求中发送自定义“Accept”标头。我怎么做?

3 个解决方案

#1


116  

Not quite. Creating a Request object does not actually send the request, and Request objects have no Read() method. (Also: read() is lowercase.) All you need to do is pass the Request as the first argument to urlopen() and that will give you your response.

不完全的。创建Request对象实际上并不发送请求,Request对象没有Read()方法。 (另外:read()是小写的。)你需要做的就是将Request作为第一个参数传递给urlopen(),这将为你提供响应。

import urllib2
request = urllib2.Request("http://www.google.com", headers={"Accept" : "text/html"})
contents = urllib2.urlopen(request).read()

#2


13  

I normally use:

我通常使用:

import urllib2

request_headers = {
"Accept-Language": "en-US,en;q=0.5",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Referer": "http://thewebsite.com",
"Connection": "keep-alive" 
}

request = urllib2.Request("http://thewebsite.com", headers=request_headers)
contents = urllib2.urlopen(request).read()
print contents

#3


1  

Beside the other solutions mentioned already, you could use add_header method.

除了已经提到的其他解决方案,您可以使用add_header方法。

So the example provided py @pantsgolem will be:

因此py @pantsgolem提供的示例将是:

import urllib2
request = urllib2.Request("http://www.google.com")

request.add_header('Accept','text/html')

##Show the header having the key 'Accept'
request.get_header('Accept')

response = urllib2.urlopen(request)
response.read()

#1


116  

Not quite. Creating a Request object does not actually send the request, and Request objects have no Read() method. (Also: read() is lowercase.) All you need to do is pass the Request as the first argument to urlopen() and that will give you your response.

不完全的。创建Request对象实际上并不发送请求,Request对象没有Read()方法。 (另外:read()是小写的。)你需要做的就是将Request作为第一个参数传递给urlopen(),这将为你提供响应。

import urllib2
request = urllib2.Request("http://www.google.com", headers={"Accept" : "text/html"})
contents = urllib2.urlopen(request).read()

#2


13  

I normally use:

我通常使用:

import urllib2

request_headers = {
"Accept-Language": "en-US,en;q=0.5",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Referer": "http://thewebsite.com",
"Connection": "keep-alive" 
}

request = urllib2.Request("http://thewebsite.com", headers=request_headers)
contents = urllib2.urlopen(request).read()
print contents

#3


1  

Beside the other solutions mentioned already, you could use add_header method.

除了已经提到的其他解决方案,您可以使用add_header方法。

So the example provided py @pantsgolem will be:

因此py @pantsgolem提供的示例将是:

import urllib2
request = urllib2.Request("http://www.google.com")

request.add_header('Accept','text/html')

##Show the header having the key 'Accept'
request.get_header('Accept')

response = urllib2.urlopen(request)
response.read()