Python3爬蟲學習筆記(2.Requests庫詳解)
阿新 • • 發佈:2018-12-27
Requests庫功能相比Urllib庫更強大,也許是自帶的
如果沒有,cmd輸入pip install requests獲取即可
例項:
import requests response = requests.get("http://www.baidu.com") print(type(response)) print(response.status_code) print(type(response.text)) print(response.text) print(response.cookies)
各種請求方式:
import requests requests.post("http://httpbin.org/post") requests.put("http://httpbin.org/put") requests.delete("http://httpbin.org/delete") requests.head("http://httpbin.org/head") requests.options("http://httpbin.org/options") requests.get("http://httpbin.org/get")
基本get請求:
import requests response = requests.get("http://httpbin.org/get") print(response.text)
帶引數get請求1:
import requests response = requests.get("http://httpbin.org/get?name=yiqing&age=18") print(response.text)
帶引數get請求2:
import requests data = { "name": "yiqing", "age": 18 } response = requests.get("http://httpbin.org/get", params=data) print(response.text)
解析JSON:
import requests response = requests.get("http://httpbin.org/get") print(response.json()) print(type(response.json()))
獲取二進位制資料(給出了一個圖片的地址,下載到本地):
import requests response = requests.get("http://p1.music.126.net/vvZLXI5EqFLsKLlvfqz0uA==/19088621370291879.jpg") with open("pic.jpg", "wb") as f: f.write(response.content)
新增headers:
import requests headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36" } response = requests.get("http://www.baidu.com", headers=headers) print(response.text)
基本post請求:
import requests data = { "name": "yiqing", "age": 18 } response = requests.post("http://httpbin.org/post", data=data) print(response.text)
帶引數post請求:
import requests data = { "name": "yiqing", "age": 18 } headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36" } response = requests.post("http://httpbin.org/post", data=data, headers=headers) print(response.json())
狀態碼判斷:
import requests response = requests.get("http://www.baidu.com") exit()if not response.status_code == requests.codes.ok else print("Requests Successfully")相當於:
import requests response = requests.get("http://www.baidu.com") exit()if not response.status_code == 200 else print("Requests Successfully")
檔案上傳(用剛才下載的圖片):
import requests files = {"file": open("pic.jpg", "rb")} response = requests.post("http://httpbin.org/post", files=files) print(response.text)
獲取cookie:
import requests response = requests.get("http://blog.csdn.net/") print(response.cookies) for key, value in response.cookies.items(): print(key+"="+value)
嘗試模擬登入:
import requests requests.get("http://httpbin.org/cookies/set/number/123456789") response = requests.get("http://www.httpbin.org/cookies") print(response.text)發現這樣登入會失敗,這裡兩次用get方式發起請求,實際上這兩次是完全獨立的過程,相當於在兩個沒有關聯的瀏覽器上進行操作的。因此獲取不到任何的cookie資訊。要做到兩次訪問用一個瀏覽器,就是下面的方法:
import requests s = requests.Session() s.get("http://httpbin.org/cookies/set/number/123456789") response = s.get("http://www.httpbin.org/cookies") print(response.text)
證書驗證:
import requests response = requests.get("https://www.12306.cn") print(response.status_code)發現出現了錯誤,原因是沒有合法證書
這裡可以避免:
import requests response = requests.get("https://www.12306.cn", verify=False) print(response.status_code)不過依然有警告資訊,再這樣操作可以去除警告資訊:
import requests from requests.packages import urllib3 urllib3.disable_warnings() response = requests.get("https://www.12306.cn", verify=False) print(response.status_code)
代理設定:
import requests proxies = { "http": "http://222.222.169.60:53281", "https": "http://222.222.169.60:53281" } response = requests.get("http://www.baidu.com", proxies=proxies) print(response.status_code)有密碼的代理:
import requests proxies = { "http": "http://user:[email protected]:53281/", } response = requests.get("http://www.baidu.com", proxies=proxies) print(response.status_code)
超時設定:
import requests response = requests.get("http://www.baidu.com", timeout=1) print(response.status_code)
抓住異常:
import requests from requests.exceptions import ConnectTimeout try: response = requests.get("http://httpbin.org/get", timeout=0.1) print(response.status_code) except ConnectTimeout: print("TIMEOUT")