基于Python模擬瀏覽器發(fā)送http請(qǐng)求
1.使用 urllib2 實(shí)現(xiàn)
#! /usr/bin/env python # -*- coding=utf-8 -*- import urllib2 url="https://www.baidu.com" req_header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11", "Accept":"text/html;q=0.9,*/*;q=0.8", "Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3", "Accept-Encoding":"gzip", "Connection":"close", "Referer":None #注意如果依然不能抓取的話,這里可以設(shè)置抓取網(wǎng)站的host } req_timeout = 5 req = urllib2.Request(url,None,req_header) resp = urllib2.urlopen(req,None,req_timeout) html = resp.read() print(html)
2.使用 requests 模塊
(1).get請(qǐng)求
#-*- coding:utf-8 -*- import requests url = "https://www.baidu.com" payload = {"key1": "value1", "key2": "value2"} r = requests.get(url, params=payload) print r.text
(2).post請(qǐng)求
#-*- coding:utf-8 -*- import requests url1 = "http://www.exanple.com/login"#登陸地址 url2 = "http://www.example.com/main"#需要登陸才能訪問(wèn)的地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = requests.post(url1, data=data, headers=headers) res2 = requests.get(url2, cookies=res1.cookies, headers=headers) print res2.content#獲得二進(jìn)制響應(yīng)內(nèi)容 print res2.raw#獲得原始響應(yīng)內(nèi)容,需要stream=True print res2.raw.read(50) print type(res2.text)#返回解碼成unicode的內(nèi)容 print res2.url print res2.history#追蹤重定向 print res2.cookies print res2.cookies["example_cookie_name"] print res2.headers print res2.headers["Content-Type"] print res2.headers.get("content-type") print res2.json#講返回內(nèi)容編碼為json print res2.encoding#返回內(nèi)容編碼 print res2.status_code#返回http狀態(tài)碼 print res2.raise_for_status()#返回錯(cuò)誤狀態(tài)碼
(3).使用session對(duì)象的寫(xiě)法
#-*- coding:utf-8 -*- import requests s = requests.Session() url1 = "http://www.exanple.com/login"#登陸地址 url2 = "http://www.example.com/main"#需要登陸才能訪問(wèn)的地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } prepped1 = requests.Request("POST", url1, data=data, headers=headers ).prepare() s.send(prepped1) """ 也可以這樣寫(xiě) res = requests.Request("POST", url1, data=data, headers=headers ) prepared = s.prepare_request(res) # do something with prepped.body # do something with prepped.headers s.send(prepared) """ prepare2 = requests.Request("POST", url2, headers=headers ).prepare() res2 = s.send(prepare2) print res2.content """另一種寫(xiě)法""" #-*- coding:utf-8 -*- import requests s = requests.Session() url1 = "http://www.exanple.com/login"#登陸地址 url2 = "http://www.example.com/main"#需要登陸才能訪問(wèn)的頁(yè)面地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = s.post(url1, data=data) res2 = s.post(url2) print(resp2.content)
3.其他的一些請(qǐng)求方式
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options(http://httpbin.org/get)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3庫(kù)numpy數(shù)組屬性的查看方法
下面小編就為大家分享一篇python3庫(kù)numpy數(shù)組屬性的查看方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python實(shí)現(xiàn)PS濾鏡的萬(wàn)花筒效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS濾鏡的萬(wàn)花筒效果,結(jié)合實(shí)例形式分析了Python基于skimage模塊操作圖片實(shí)現(xiàn)PS濾鏡萬(wàn)花筒效果的原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01詳解OpenCV中簡(jiǎn)單的鼠標(biāo)事件處理
談及鼠標(biāo)事件,就是在觸發(fā)鼠標(biāo)按鈕后程序所做出相應(yīng)的反應(yīng),但是不影響程序的整個(gè)線程。本文將主要介紹OpenCV中的簡(jiǎn)單鼠標(biāo)事件處理,感興趣的可以學(xué)習(xí)一下2022-01-01基于Python+Turtle實(shí)現(xiàn)繪制簡(jiǎn)易的大風(fēng)車
大風(fēng)車,吱呀吱呦呦地轉(zhuǎn),這里的風(fēng)景呀真好看!天好看,地好看……一首熟悉的歌曲,是否已經(jīng)把你拉回了童年?本文將用Turtle庫(kù)繪制簡(jiǎn)易的大風(fēng)車,需要的可以參考一下2022-06-06flask框架使用orm連接數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了flask框架使用orm連接數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了flask框架使用flask_sqlalchemy包進(jìn)行mysql數(shù)據(jù)庫(kù)連接操作的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-07-07Python設(shè)計(jì)模式之工廠模式簡(jiǎn)單示例
這篇文章主要介紹了Python設(shè)計(jì)模式之工廠模式,簡(jiǎn)單說(shuō)明了工廠模式的原理,并結(jié)合實(shí)例形式給出了Python實(shí)現(xiàn)工廠模式的具體操作技巧,需要的朋友可以參考下2018-01-01