2019-05-05 17:09:04 3097浏览
本篇文章扣丁学堂Python培训小编给读者们分享一下Python发送带header的http请求方法,对Python开发技术感兴趣的小伙伴就随小编来了解一下Python发送带header的http请求方法详解,感兴趣的小伙伴就来了解一下吧。
简单的header
包含较多元素的header
import urllib2
request = urllib2.Request('http://example.com/')
request.add_header('User-Agent', 'fake-client')
response = urllib2.urlopen(request)
print request.read()
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开发方面内容的小伙伴,请关注扣丁学堂Python培训官网、微信等平台,扣丁学堂IT职业在线学习教育有专业的Python讲师为您指导,此外扣丁学堂老师精心推出的Python视频教程定能让你快速掌握Python从入门到精通开发实战技能。扣丁学堂Python技术交流群:279521237。
【关注微信公众号获取更多学习资料】 【扫码进入Python全栈开发免费公开课】