Python requests模塊實(shí)例用法
1、Requests模塊說明
Requests 是使用 Apache2 Licensed 許可證的 HTTP 庫。用 Python 編寫,真正的為人類著想。
Python 標(biāo)準(zhǔn)庫中的 urllib2 模塊提供了你所需要的大多數(shù) HTTP 功能,但是它的 API 太渣了。它是為另一個(gè)時(shí)代、另一個(gè)互聯(lián)網(wǎng)所創(chuàng)建的。它需要巨量的工作,甚至包括各種方法覆蓋,來完成最簡(jiǎn)單的任務(wù)。
在Python的世界里,事情不應(yīng)該這么麻煩。
Requests 使用的是 urllib3,因此繼承了它的所有特性。Requests 支持 HTTP 連接保持和連接池,支持使用 cookie 保持會(huì)話,支持文件上傳,支持自動(dòng)確定響應(yīng)內(nèi)容的編碼,支持國(guó)際化的 URL 和 POST 數(shù)據(jù)自動(dòng)編碼?,F(xiàn)代、國(guó)際化、人性化。
2、Requests模塊安裝
然后執(zhí)行安裝
$ python setup.py install
個(gè)人推薦使用pip安裝
pip install requests
也可以使用easy_install安裝
easy_install requests
嘗試在IDE中import requests,如果沒有報(bào)錯(cuò),那么安裝成功。
3、Requests模塊簡(jiǎn)單入門
#HTTP請(qǐng)求類型
#get類型
r = requests.get('https://github.com/timeline.json')
#post類型
r = requests.post("http://m.ctrip.com/post")
#put類型
r = requests.put("http://m.ctrip.com/put")
#delete類型
r = requests.delete("http://m.ctrip.com/delete")
#head類型
r = requests.head("http://m.ctrip.com/head")
#options類型
r = requests.options("http://m.ctrip.com/get")
#獲取響應(yīng)內(nèi)容
print r.content #以字節(jié)的方式去顯示,中文顯示為字符
print r.text #以文本的方式去顯示
#URL傳遞參數(shù)
payload = {'keyword': '日本', 'salecityid': '2'}
r = requests.get("http://m.ctrip.com/webapp/tourvisa/visa_list", params=payload)
print r.url #示例為http://m.ctrip.com/webapp/tourvisa/visa_list?salecityid=2&keyword=日本
#獲取/修改網(wǎng)頁編碼
r = requests.get('https://github.com/timeline.json')
print r.encoding
r.encoding = 'utf-8'
#json處理
r = requests.get('https://github.com/timeline.json')
print r.json() #需要先import json
#定制請(qǐng)求頭
url = 'http://m.ctrip.com'
headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
r = requests.post(url, headers=headers)
print r.request.headers
#復(fù)雜post請(qǐng)求
url = 'http://m.ctrip.com'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload)) #如果傳遞的payload是string而不是dict,需要先調(diào)用dumps方法格式化一下
#post多部分編碼文件
url = 'http://m.ctrip.com'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
#響應(yīng)狀態(tài)碼
r = requests.get('http://m.ctrip.com')
print r.status_code
#響應(yīng)頭
r = requests.get('http://m.ctrip.com')
print r.headers
print r.headers['Content-Type']
print r.headers.get('content-type') #訪問響應(yīng)頭部分內(nèi)容的兩種方式
#Cookies
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
r.cookies['example_cookie_name'] #讀取cookies
url = 'http://m.ctrip.com/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies) #發(fā)送cookies
#設(shè)置超時(shí)時(shí)間
r = requests.get('http://m.ctrip.com', timeout=0.001)
#設(shè)置訪問代理
proxies = {
"http": "http://10.10.10.10:8888",
"https": "http://10.10.10.100:4444",
}
r = requests.get('http://m.ctrip.com', proxies=proxies)
xml請(qǐng)求
#!/user/bin/env python
#coding=utf-8
import requests
class url_request():
def __init__(self):
""" init """
if __name__=='__main__':
headers = {'Content-type': 'text/xml'}
XML = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Request xmlns="http://tempuri.org/"><jme><JobClassFullName>WeChatJSTicket.JobWS.Job.JobRefreshTicket,WeChatJSTicket.JobWS</JobClassFullName><Action>RUN</Action><Param>1</Param><HostIP>127.0.0.1</HostIP><JobInfo>1</JobInfo><NeedParallel>false</NeedParallel></jme></Request></soap:Body></soap:Envelope>'
url = 'http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx'
r = requests.post(url,headers=headers,data=XML)
#r.encoding = 'utf-8'
data = r.text
print data
相關(guān)文章
探索Python元類的魅力:靈活定制類的創(chuàng)建過程
在Python編程中,元類(Metaclass)是一項(xiàng)高級(jí)特性,它允許我們?cè)诙x類的時(shí)候動(dòng)態(tài)地控制類的創(chuàng)建過程。元類提供了一種強(qiáng)大的機(jī)制,可以對(duì)類進(jìn)行定制化,擴(kuò)展其功能,并在類的實(shí)例化過程中執(zhí)行額外的操作,本文將深入解析2023-10-10
如何解決Pycharm運(yùn)行報(bào)錯(cuò)No Python interpreter selected
這篇文章主要介紹了如何解決Pycharm運(yùn)行時(shí)No Python interpreter selected問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Python?4種實(shí)現(xiàn)定時(shí)任務(wù)的方案
這篇文章主要給大家分享了Python?4種實(shí)現(xiàn)定時(shí)任務(wù)的方案,運(yùn)用 while True: + sleep()、Timeloop 庫、threading.Timer 、內(nèi)置模塊 sched ,下面就來看看具體的實(shí)現(xiàn)過程吧2021-12-12
利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化(看看哪家最劃算)
通過python對(duì)數(shù)據(jù)進(jìn)行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09
Pytorch數(shù)據(jù)類型Tensor張量操作的實(shí)現(xiàn)
本文主要介紹了Pytorch數(shù)據(jù)類型Tensor張量操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

