1、Request模塊python
1.HTTP for Humans,更簡潔更友好git
2.繼承了urllib全部的特徵github
3.底層使用的是urllib3web
4.開源地址:https://github.com/requests/requestsjson
5.中文文檔:https://requests.readthedocs.io/zh_CN/latest/安全
6.先安裝這個包:pip install requests微信
7.get請求post
(1)requests.get()學習
(2)requests.request("get",url)大數據
(3)能夠帶有headers和params參數
8.get返回內容
import requests #兩種請求 url = "http://www.baidu.com/s?" rsp = requests.get(url) # print(rsp.text) #使用get請求 rsp = requests.request("GET",url) # print(rsp.text) #拿到的結果都是同樣的 """ 使用參數headers和params來研究返回結果 """ kw = { "wd":"python中文文檔" } headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36" } rsp=requests.get(url,params=kw,headers=headers) print(rsp.text) print("=========================") print(rsp.content) print("=========================") print(rsp.url) print(rsp.encoding) print(rsp.status_code)
2、post
data,headers要求使用dict
import requests from urllib import parse import json url = "http://www.baidu.com/s?" data = { "kw":"girl" } # data = parse.urlencode(data).encode("utf-8") headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36" } rsp = requests.post(url,data=data,headers=headers) print(rsp.text) # print(rsp.json())
3、proxy代理
1.代理有可能會報錯,若是使用人數多,考慮到安全問題,可能會被強行關閉
2.用戶驗證
(1)代理驗證:可能須要HTTP basic Auth
proxy = {"http":"china:123456@192.168.1.123:458"} #格式爲 用戶名:密碼@代理地址:端口地址 rsp = requests.get("http://baidu.com",proxies=proxy)
(2)web客戶端驗證
若是遇到web客戶端驗證,須要添加auth=(用戶名,密碼)
auth = {"test","123456"}#受權信息 rsp = requests.get("http://www.baidu.com",auth=auth)
4、源碼
Reptitle10_1_Requests.py
Reptitle10_2_Post.py
https://github.com/ruigege66/PythonReptile/blob/master/Reptitle10_1_Requests.py
https://github.com/ruigege66/PythonReptile/blob/master/Reptitle10_2_Post.py
2.CSDN:https://blog.csdn.net/weixin_44630050
3.博客園:https://www.cnblogs.com/ruigege0000/
4.歡迎關注微信公衆號:傅里葉變換,我的公衆號,僅用於學習交流,後臺回覆」禮包「,獲取大數據學習資料