I am pretty new to python's urllib. What I need to do is set a custom header for the request being sent to the server. Specifically, I need to set the Content-type and Authorizations headers. I have looked into the python documentation, but I haven't been able to find it.
我对python的urllib很陌生。我需要做的是为发送到服务器的请求设置一个自定义标头。具体地说,我需要设置内容类型和授权头。我已经研究了python文档,但是还没有找到它。
4 个解决方案
#1
68
adding HTTP headers using urllib2:
使用urllib2添加HTTP头文件:
from the docs:
从文档:
import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
resp = urllib2.urlopen(req)
content = resp.read()
#2
49
For both Python 3 and Python 2, this works:
对于Python 3和Python 2,这都可以:
try:
from urllib.request import Request, urlopen # Python 3
except ImportError:
from urllib2 import Request, urlopen # Python 2
q = Request('http://api.company.com/items/details?country=US&language=en')
q.add_header('apikey', 'xxx')
a = urlopen(q).read()
print(a)
#3
9
Use urllib2 and create a Request object which you then hand to urlopen. http://docs.python.org/library/urllib2.html
使用urllib2并创建一个请求对象,然后将其交给urlopen。http://docs.python.org/library/urllib2.html
I dont really use the "old" urllib anymore.
我真的不再用“旧”的小熊了。
req = urllib2.Request("http://google.com", None, {'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
response = urllib2.urlopen(req).read()
untested....
未经考验的....
#4
1
For multiple headers do as follow:
对于多个头文件,请执行以下操作:
import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('param1', '212212')
req.add_header('param2', '12345678')
req.add_header('other_param1', 'sample')
req.add_header('other_param2', 'sample1111')
req.add_header('and_any_other_parame', 'testttt')
resp = urllib2.urlopen(req)
content = resp.read()
#1
68
adding HTTP headers using urllib2:
使用urllib2添加HTTP头文件:
from the docs:
从文档:
import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
resp = urllib2.urlopen(req)
content = resp.read()
#2
49
For both Python 3 and Python 2, this works:
对于Python 3和Python 2,这都可以:
try:
from urllib.request import Request, urlopen # Python 3
except ImportError:
from urllib2 import Request, urlopen # Python 2
q = Request('http://api.company.com/items/details?country=US&language=en')
q.add_header('apikey', 'xxx')
a = urlopen(q).read()
print(a)
#3
9
Use urllib2 and create a Request object which you then hand to urlopen. http://docs.python.org/library/urllib2.html
使用urllib2并创建一个请求对象,然后将其交给urlopen。http://docs.python.org/library/urllib2.html
I dont really use the "old" urllib anymore.
我真的不再用“旧”的小熊了。
req = urllib2.Request("http://google.com", None, {'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
response = urllib2.urlopen(req).read()
untested....
未经考验的....
#4
1
For multiple headers do as follow:
对于多个头文件,请执行以下操作:
import urllib2
req = urllib2.Request('http://www.example.com/')
req.add_header('param1', '212212')
req.add_header('param2', '12345678')
req.add_header('other_param1', 'sample')
req.add_header('other_param2', 'sample1111')
req.add_header('and_any_other_parame', 'testttt')
resp = urllib2.urlopen(req)
content = resp.read()