亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python requests模塊實(shí)例用法

 更新時間:2019年02月11日 08:48:46   投稿:laozhang  
在本篇文章中小編給大家分享了關(guān)于Python requests模塊實(shí)例用法,有需要的朋友們學(xué)習(xí)參考下。

1、Requests模塊說明

Requests 是使用 Apache2 Licensed 許可證的 HTTP 庫。用 Python 編寫,真正的為人類著想。

Python 標(biāo)準(zhǔn)庫中的 urllib2 模塊提供了你所需要的大多數(shù) HTTP 功能,但是它的 API 太渣了。它是為另一個時代、另一個互聯(lián)網(wǎng)所創(chuàng)建的。它需要巨量的工作,甚至包括各種方法覆蓋,來完成最簡單的任務(wù)。

在Python的世界里,事情不應(yīng)該這么麻煩。

Requests 使用的是 urllib3,因此繼承了它的所有特性。Requests 支持 HTTP 連接保持和連接池,支持使用 cookie 保持會話,支持文件上傳,支持自動確定響應(yīng)內(nèi)容的編碼,支持國際化的 URL 和 POST 數(shù)據(jù)自動編碼?,F(xiàn)代、國際化、人性化。

2、Requests模塊安裝

點(diǎn)此下載

然后執(zhí)行安裝

$ python setup.py install

個人推薦使用pip安裝

pip install requests

也可以使用easy_install安裝

easy_install requests

嘗試在IDE中import requests,如果沒有報錯,那么安裝成功。

3、Requests模塊簡單入門

#HTTP請求類型
#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 

#定制請求頭
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請求
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è)置超時時間
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請求

#!/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代碼實(shí)現(xiàn)小程序登錄流程時序總結(jié)

    python代碼實(shí)現(xiàn)小程序登錄流程時序總結(jié)

    這篇文章主要為大家介紹了python代碼實(shí)現(xiàn)小程序的登錄案例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • python返回昨天日期的方法

    python返回昨天日期的方法

    這篇文章主要介紹了python返回昨天日期的方法,涉及Python日期操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 深入理解python Matplotlib庫的高級特性

    深入理解python Matplotlib庫的高級特性

    Matplotlib是一款極其強(qiáng)大的Python數(shù)據(jù)可視化庫,這篇文章中,我們將深入討論 Matplotlib 的一些高級特性,包括對象導(dǎo)向接口、自定義顏色映射和樣式、動態(tài)圖形等,感興趣的小伙伴跟著小編一起來探討吧
    2023-07-07
  • Python 中的函數(shù)裝飾器和閉包詳解

    Python 中的函數(shù)裝飾器和閉包詳解

    這篇文章主要介紹了Python 中的函數(shù)裝飾器和閉包詳解,需要的朋友可以參考下
    2021-02-02
  • python-json校驗(yàn)-jsonpath解析

    python-json校驗(yàn)-jsonpath解析

    這篇文章主要介紹了python-json校驗(yàn)-jsonpath,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Pycharm配置opencv與numpy的實(shí)現(xiàn)

    Pycharm配置opencv與numpy的實(shí)現(xiàn)

    本文總結(jié)了兩種方法來導(dǎo)入opencv與numpy包,第一種是直接在Pycharm中導(dǎo)入兩個包,第二種是在官網(wǎng)下載相關(guān)文件進(jìn)行配置,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫簡單實(shí)現(xiàn)注冊登錄

    Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫簡單實(shí)現(xiàn)注冊登錄

    這篇文章主要介紹了Python?+?Tkinter連接本地MySQL數(shù)據(jù)庫簡單實(shí)現(xiàn)注冊登錄。下面文章著情介紹,需要的小伙伴可以參考一下
    2022-01-01
  • Python函數(shù)命名空間,作用域LEGB及Global詳析

    Python函數(shù)命名空間,作用域LEGB及Global詳析

    這篇文章主要介紹了Python函數(shù)命名空間,作用域LEGB及Global詳析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-09-09
  • numpy實(shí)現(xiàn)RNN原理實(shí)現(xiàn)

    numpy實(shí)現(xiàn)RNN原理實(shí)現(xiàn)

    這篇文章主要介紹了numpy實(shí)現(xiàn)RNN原理實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python實(shí)現(xiàn)excel公式格式化的示例代碼

    python實(shí)現(xiàn)excel公式格式化的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)excel公式格式化的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評論