对Python发送带header的http请求方法详解
更新时间:2019年01月02日 10:47:28 作者:平常心lzt
今天小编就为大家分享一篇对Python发送带header的http请求方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
简单的header
import urllib2
request = urllib2.Request('http://example.com/')
request.add_header('User-Agent', 'fake-client')
response = urllib2.urlopen(request)
print request.read()
包含较多元素的header
import urllib,urllib2
url = 'http://example.com/'
headers = { 'Host':'example.com',
'Connection':'keep-alive',
'Cache-Control':'max-age=0',
'Accept': 'text/html, */*; q=0.01',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
'DNT':'1',
'Referer': 'http://example.com/',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'
}
data = None
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
html = response.read()
以上这篇对Python发送带header的http请求方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
python Autopep8实现按PEP8风格自动排版Python代码
这篇文章主要介绍了python Autopep8实现按PEP8风格自动排版Python代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-03-03


最新评论