国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python requests模塊實例用法

Python requests模塊實例用法

2021-05-26 00:41腳本之家 Python

在本篇文章中小編給大家分享了關于Python requests模塊實例用法,有需要的朋友們學習參考下。

1、Requests模塊說明

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

Python 標準庫中的 urllib2 模塊提供了你所需要的大多數 HTTP 功能,但是它的 API 太渣了。它是為另一個時代、另一個互聯網所創建的。它需要巨量的工作,甚至包括各種方法覆蓋,來完成最簡單的任務。

在Python的世界里,事情不應該這么麻煩。

Requests 使用的是 urllib3,因此繼承了它的所有特性。Requests 支持 HTTP 連接保持和連接池,支持使用 cookie 保持會話,支持文件上傳,支持自動確定響應內容的編碼,支持國際化的 URL 和 POST 數據自動編碼。現代、國際化、人性化。

2、Requests模塊安裝

點此下載

然后執行安裝

?
1
$ python setup.py install

個人推薦使用pip安裝

?
1
pip install requests

也可以使用easy_install安裝

?
1
easy_install requests

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

3、Requests模塊簡單入門

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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")
 
#獲取響應內容
print r.content #以字節的方式去顯示,中文顯示為字符
print r.text #以文本的方式去顯示
 
#URL傳遞參數
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=日本
 
#獲取/修改網頁編碼
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
 
#復雜post請求
url = 'http://m.ctrip.com'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload)) #如果傳遞的payload是string而不是dict,需要先調用dumps方法格式化一下
 
#post多部分編碼文件
url = 'http://m.ctrip.com'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
 
#響應狀態碼
r = requests.get('http://m.ctrip.com')
print r.status_code
 
#響應頭
r = requests.get('http://m.ctrip.com')
print r.headers
print r.headers['Content-Type']
print r.headers.get('content-type') #訪問響應頭部分內容的兩種方式
 
#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) #發送cookies
 
#設置超時時間
r = requests.get('http://m.ctrip.com', timeout=0.001)
 
#設置訪問代理
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請求

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/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

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级黄色大片在线观看 | √天堂8在线网 | jizzhd中国| 337p日本粉嫩噜噜噜 | 国产亚洲精品久久19p | 亚洲黄色成人av | 免费一级毛片在线播放放视频 | 婷婷亚洲五月 | 激情免费视频 | 黄色片网站在线免费观看 | 亚洲成人高清 | 在线一级视频 | 视频一区在线 | 91精品久久久久久久91蜜桃 | 求av网址| 久久成人免费视频 | 午夜网 | 国产精品久久国产精品 | 国产黄色片一级 | 久久美女| 色接久久| 午夜精品视频在线观看 | 久久久久久一区 | 午夜专区 | 国产精品福利视频 | 欧美日韩中| 成人精品国产 | 精品久久久久久久 | 最新日韩av| 国产精品福利视频 | www.久久久.com | 福利在线看 | 日韩三级| 成人av一区二区三区 | 国产成人av在线播放 | 91在线网站| 色悠悠视频 | 国产精品久久久久一区二区三区 | 中国一级特黄毛片大片 | 国产成人亚洲综合 | 久久九|