1 import requests
2 import json
3
4
5 # 1、HTTP方法
6 requests.get('https://github.com/timeline.json') #GET请求
7 requests.post('http://httpbin.org/post') #POST请求
8 requests.put('http://httpbin.org/put') #PUT请求
9 requests.delete('http://httpbin.org/delete') #DELETE请求
10 requests.head('http://httpbin.org/get') #HEAD请求
11 requests.options('http://httpbin.org/get') #OPTIONS请求
12
13
14 # 2、Make a Request
15 url = 'http://www.baidu.com'
16 req = requests.get(url)
17 print(req.text)
18
19
20 # 3、response属性
21 import requests
22 response = requests.get('http://www.baidu.com/s', params={'wd': 'python'}) # GET参数实例
23 print(response.url)
24 print(response.text) #返回的内容,字符串形式
25 print(response.status_code) #返回码
26 print(response.content) #返回的内容,二进制形式
27 print(response.headers)
28 print(response.headers['content-type'])
29 print(response.headers.get('content-type'))
30 print(response.encoding)
31 print(response.json())
32 response.raise_for_status() #抛出异常 非200响应
33
34
35 # 4、get带参数
36 payload = {'key1': 'value1', 'key2': 'value2'}
37 r = requests.get('http://httpbin.org/get', params=payload)
38 print(r.url)
39 print(r.text)
40
41
42 # 5、数据流方式读取结果
43 r = requests.get('https://api.github.com/events', stream=True)
44 print(r.raw) #不解码
45 print(r.raw.read(10))
46 with open('output.txt', 'wb') as fd:
47 for chunk in r.iter_content(chunk_size=128): #解码
48 fd.write(chunk)
49
50
51 # 6、自定义头信息
52 url = 'https://api.github.com/some/endpoint'
53 headers = {'user-agent': 'my-app/0.0.1'} # 字符串,字节串,Unicode
54 r = requests.get(url, headers=headers)
55
56
57 # 7、post数据:字典
58 payload = {'key1': 'value1', 'key2': 'value2'}
59 r = requests.post("http://httpbin.org/post", data=payload)
60 print(r.text)
61
62
63 # 8、post数据:列表
64 payload = (('key1', 'value1'), ('key1', 'value2'))
65 r = requests.post('http://httpbin.org/post', data=payload)
66 print(r.text)
67
68
69 # 9、post数据:字符串
70 url = 'http://httpbin.org/post'
71 payload = {'some': 'data'}
72 r = requests.post(url, data=json.dumps(payload))
73 print(r.text)
74
75
76 # 10、post数据:字符串
77 url = 'http://httpbin.org/post'
78 payload = {'some': 'data'}
79 r = requests.post(url, json=payload)
80 print(r.text)
81
82
83 # 11、上传多部分编码的文件
84 url = 'http://httpbin.org/post'
85 files = {'file': open('a.xls', 'rb')}
86 r = requests.post(url, files=files)
87 print(r.text)
88
89
90 # 12、上传多部分编码的文件
91 url = 'http://httpbin.org/post'
92 files = {'file': ('a.xls', open('a.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
93 r = requests.post(url, files=files)
94 print(r.text)
95
96
97 # 13、字符串当文件发
98 url = 'http://httpbin.org/post'
99 files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
100 r = requests.post(url, files=files)
101 print(r.text)
102
103
104 # 14、cookies
105 url = 'http://httpbin.org/cookies'
106 cookies = dict(cookies_are='working')
107 r = requests.get(url, cookies=cookies)
108 print(r.text)
109
110
111 # 15、cookies
112 jar = requests.cookies.RequestsCookieJar()
113 jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
114 jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
115 url = 'http://httpbin.org/cookies'
116 r = requests.get(url, cookies=jar)
117 print(r.text)
118
119
120 # 16、timeout
121 requests.get('http://github.com', timeout=0.001)