本篇文章為大家展示了怎么在python中安裝requests庫,代碼簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
requests是python實(shí)現(xiàn)的簡單易用的HTTP庫,使用起來比urllib簡潔很多
因?yàn)槭堑谌綆欤允褂们靶枰猚md安裝
pip install requests
安裝完成后import一下,正常則說明可以開始使用了。
基本用法:
requests.get()用于請求目標(biāo)網(wǎng)站,類型是一個HTTPresponse類型
import requests response = requests.get('http://www.baidu.com') print(response.status_code) # 打印狀態(tài)碼 print(response.url) # 打印請求url print(response.headers) # 打印頭信息 print(response.cookies) # 打印cookie信息print(response.text) #以文本形式打印網(wǎng)頁源碼 print(response.content) #以字節(jié)流形式打印
運(yùn)行結(jié)果:
狀態(tài)碼:200
各種請求方式:
import requests requests.get('http://httpbin.org/get') requests.post('http://httpbin.org/post') requests.put('http://httpbin.org/put') requests.delete('http://httpbin.org/delete') requests.head('http://httpbin.org/get') requests.options('http://httpbin.org/get')
基本的get請求
import requests response = requests.get('http://httpbin.org/get')print(response.text)
帶參數(shù)的GET請求:
第一種直接將參數(shù)放在url內(nèi)
import requests response = requests.get(http://httpbin.org/get?name=gemey&age=22)print(response.text)
解析json
import requests response = requests.get('http://httpbin.org/get') print(response.text) print(response.json()) #response.json()方法同json.loads(response.text) print(type(response.json()))
上述內(nèi)容就是怎么在python中安裝requests庫,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:怎么在python中安裝requests庫-創(chuàng)新互聯(lián)
文章來源:http://www.chinadenli.net/article12/dpjpgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、建站公司、企業(yè)網(wǎng)站制作、ChatGPT、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容