知乎首發(fā)地址:https://zhuanlan.zhihu.com/p/139105313
Requests?allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic, thanks to urllib3.
一、Requests簡(jiǎn)介
中文幫助文檔:http://cn.python-requests.org/zh_CN/latest/
Requests的特點(diǎn),此部分內(nèi)容直接摘自官網(wǎng)遥皂,以免翻譯的不準(zhǔn)確。
Keep-Alive & Connection Pooling
International Domains and URLs
Sessions with Cookie Persistence
Browser-style SSL Verification
Automatic Content Decoding
Basic/Digest Authentication
Elegant Key/Value Cookies
Automatic Decompression
Unicode Response Bodies
HTTP(S) Proxy Support
Multipart File Uploads
Streaming Downloads
Connection Timeouts
Chunked Requests
Requests officially supports Python 2.7 & 3.4–3.7, and runs great on PyPy.
二滋尉、requests的安裝
下載python3的windows安裝包拧揽,直接安裝即可休里。
查看環(huán)境變量:我的電腦->屬性->高級(jí)系統(tǒng)設(shè)置->環(huán)境變量->用戶環(huán)境變量->path
檢查python是否安裝成功:cmd->python富腊,出現(xiàn)下圖即可坏逢。
CMD命令行,使用pip安裝:pip3 install requests
安裝requests:Cmd->python下輸入import requests
如果沒有提示錯(cuò)誤赘被,則表明已經(jīng)安裝成功了
三是整、使用requests進(jìn)行第一個(gè)接口測(cè)試
1、打開CMD->python
2帘腹、依次輸入下面指令:
import requests # 導(dǎo)入requests包
r = requests.get(‘https://www.baidu.com’) #獲得請(qǐng)求的url
r.status_code # 返回碼
r.headers['content-type'] # 返回頭部信息
r.encoding # 編碼信息
r.text #內(nèi)容部分(PS,由于編碼問題许饿,建議這里使用r.content)
r.content #內(nèi)容部分
3阳欲、響應(yīng)結(jié)果對(duì)象常見屬性介紹
response.url:返回請(qǐng)求網(wǎng)站的 url
response.status_code:返回響應(yīng)的狀態(tài)碼
response.encoding:返回響應(yīng)的編碼方式
response.cookies:返回響應(yīng)的 Cookie 信息
response.headers:返回響應(yīng)頭
response.content:返回 bytes 類型的響應(yīng)體
response.text:返回 str 類型的響應(yīng)體,相當(dāng)于 response.content.decode('utf-8’)
附圖接口測(cè)試其他工具的知識(shí)腦圖陋率,可以完善我們測(cè)試的工具知識(shí)體系球化,如果知友對(duì)這部分內(nèi)容感興趣,可以持續(xù)關(guān)注小編的賬號(hào)瓦糟,除此之外筒愚,小編還錄制了不少這方面的技術(shù)視頻,知友如果有需要菩浙,可以私聊本小編獲取哦巢掺!
歡迎關(guān)注作者,如果覺得寫的還不錯(cuò)劲蜻,就給點(diǎn)個(gè)贊同陆淀、喜歡、收藏(后續(xù)持續(xù)更新)先嬉。