2019-05-05 17:09:04 3995瀏覽
本篇文章扣丁學堂Python培訓小編給讀者們分享一下Python發(fā)送帶header的http請求方法,對Python開發(fā)技術(shù)感興趣的小伙伴就隨小編來了解一下Python發(fā)送帶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()
想要了解更多關(guān)于Python開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學堂Python培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的Python講師為您指導,此外扣丁學堂老師精心推出的Python視頻教程定能讓你快速掌握Python從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂Python技術(shù)交流群:279521237。
【關(guān)注微信公眾號獲取更多學習資料】 【掃碼進入Python全棧開發(fā)免費公開課】