Skip to content

Commit 4d79de0

Browse files
committed
添加了requests
1 parent 2c8917b commit 4d79de0

File tree

5 files changed

+322
-0
lines changed

5 files changed

+322
-0
lines changed

11. useful tools/11.10 requests.ipynb

Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# requests 模块:HTTP for Human"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": [
18+
"import requests"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {
24+
"collapsed": true
25+
},
26+
"source": [
27+
"Python 标准库中的 `urllib2` 模块提供了你所需要的大多数 `HTTP` 功能,但是它的 `API` 不是特别方便使用。\n",
28+
"\n",
29+
"`requests` 模块号称 `HTTP for Human`,它可以这样使用:"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 2,
35+
"metadata": {
36+
"collapsed": false
37+
},
38+
"outputs": [],
39+
"source": [
40+
"r = requests.get(\"http://httpbin.org/get\")\n",
41+
"r = requests.post('http://httpbin.org/post', data = {'key':'value'})\n",
42+
"r = requests.put(\"http://httpbin.org/put\")\n",
43+
"r = requests.delete(\"http://httpbin.org/delete\")\n",
44+
"r = requests.head(\"http://httpbin.org/get\")\n",
45+
"r = requests.options(\"http://httpbin.org/get\")"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"## 传入 URL 参数"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"metadata": {},
58+
"source": [
59+
"假如我们想访问 `httpbin.org/get?key=val`,我们可以使用 `params` 传入这些参数:"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 3,
65+
"metadata": {
66+
"collapsed": true
67+
},
68+
"outputs": [],
69+
"source": [
70+
"payload = {'key1': 'value1', 'key2': 'value2'}\n",
71+
"r = requests.get(\"http://httpbin.org/get\", params=payload)"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"查看 `url` :"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 4,
84+
"metadata": {
85+
"collapsed": false
86+
},
87+
"outputs": [
88+
{
89+
"name": "stdout",
90+
"output_type": "stream",
91+
"text": [
92+
"http://httpbin.org/get?key2=value2&key1=value1\n"
93+
]
94+
}
95+
],
96+
"source": [
97+
"print(r.url)"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"## 读取响应内容"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"`Requests` 会自动解码来自服务器的内容。大多数 `unicode` 字符集都能被无缝地解码。"
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 5,
117+
"metadata": {
118+
"collapsed": false
119+
},
120+
"outputs": [
121+
{
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"{\"message\":\"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.\",\"documentation_url\":\"https://developer.github.com/v3/activity/events/#list-public-events\"}\n"
126+
]
127+
}
128+
],
129+
"source": [
130+
"r = requests.get('https://github.com/timeline.json')\n",
131+
"\n",
132+
"print r.text"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"查看文字编码:"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 6,
145+
"metadata": {
146+
"collapsed": false
147+
},
148+
"outputs": [
149+
{
150+
"data": {
151+
"text/plain": [
152+
"'utf-8'"
153+
]
154+
},
155+
"execution_count": 6,
156+
"metadata": {},
157+
"output_type": "execute_result"
158+
}
159+
],
160+
"source": [
161+
"r.encoding"
162+
]
163+
},
164+
{
165+
"cell_type": "markdown",
166+
"metadata": {},
167+
"source": [
168+
"每次改变文字编码,`text` 的内容也随之变化:"
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": 7,
174+
"metadata": {
175+
"collapsed": false
176+
},
177+
"outputs": [
178+
{
179+
"data": {
180+
"text/plain": [
181+
"u'{\"message\":\"Hello there, wayfaring stranger. If you\\xe2\\x80\\x99re reading this then you probably didn\\xe2\\x80\\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.\",\"documentation_url\":\"https://developer.github.com/v3/activity/events/#list-public-events\"}'"
182+
]
183+
},
184+
"execution_count": 7,
185+
"metadata": {},
186+
"output_type": "execute_result"
187+
}
188+
],
189+
"source": [
190+
"r.encoding = \"ISO-8859-1\"\n",
191+
"\n",
192+
"r.text"
193+
]
194+
},
195+
{
196+
"cell_type": "markdown",
197+
"metadata": {},
198+
"source": [
199+
"`Requests` 中也有一个内置的 `JSON` 解码器处理 `JSON` 数据:"
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 8,
205+
"metadata": {
206+
"collapsed": false
207+
},
208+
"outputs": [
209+
{
210+
"data": {
211+
"text/plain": [
212+
"{u'documentation_url': u'https://developer.github.com/v3/activity/events/#list-public-events',\n",
213+
" u'message': u'Hello there, wayfaring stranger. If you\\xe2\\x80\\x99re reading this then you probably didn\\xe2\\x80\\x99t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.'}"
214+
]
215+
},
216+
"execution_count": 8,
217+
"metadata": {},
218+
"output_type": "execute_result"
219+
}
220+
],
221+
"source": [
222+
"r.json()"
223+
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {},
228+
"source": [
229+
"如果 `JSON` 解码失败, `r.json` 就会抛出一个异常。"
230+
]
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"## 响应状态码"
237+
]
238+
},
239+
{
240+
"cell_type": "code",
241+
"execution_count": 9,
242+
"metadata": {
243+
"collapsed": false
244+
},
245+
"outputs": [
246+
{
247+
"data": {
248+
"text/plain": [
249+
"407"
250+
]
251+
},
252+
"execution_count": 9,
253+
"metadata": {},
254+
"output_type": "execute_result"
255+
}
256+
],
257+
"source": [
258+
"r = requests.get('http://httpbin.org/get')\n",
259+
"\n",
260+
"r.status_code"
261+
]
262+
},
263+
{
264+
"cell_type": "markdown",
265+
"metadata": {},
266+
"source": [
267+
"## 响应头"
268+
]
269+
},
270+
{
271+
"cell_type": "code",
272+
"execution_count": 10,
273+
"metadata": {
274+
"collapsed": false
275+
},
276+
"outputs": [
277+
{
278+
"data": {
279+
"text/plain": [
280+
"'text/html'"
281+
]
282+
},
283+
"execution_count": 10,
284+
"metadata": {},
285+
"output_type": "execute_result"
286+
}
287+
],
288+
"source": [
289+
"r.headers['Content-Type']"
290+
]
291+
}
292+
],
293+
"metadata": {
294+
"kernelspec": {
295+
"display_name": "Python 2",
296+
"language": "python",
297+
"name": "python2"
298+
},
299+
"language_info": {
300+
"codemirror_mode": {
301+
"name": "ipython",
302+
"version": 2
303+
},
304+
"file_extension": ".py",
305+
"mimetype": "text/x-python",
306+
"name": "python",
307+
"nbconvert_exporter": "python",
308+
"pygments_lexer": "ipython2",
309+
"version": "2.7.6"
310+
}
311+
},
312+
"nbformat": 4,
313+
"nbformat_minor": 0
314+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,7 @@ conda update anaconda
205205
- [11.06 gzip, zipfile, tarfile 模块:处理压缩文件](11. useful tools/11.06 gzip, zipfile, tarfile.ipynb)
206206
- [11.07 logging 模块:记录日志](11. useful tools/11.07 logging.ipynb)
207207
- [11.08 string 模块:字符串处理](11. useful tools/11.08 string.ipynb)
208+
- [11.09 collections 模块:更多数据结构](11. useful tools/11.09 collections.ipynb)
209+
- [11.10 requests 模块:HTTP for Human](11. useful tools/11.10 requests.ipynb)
208210
- [12. **Pandas**](12. pandas)
209211
- [12.01 十分钟上手 Pandas](12. pandas/12.01 ten minutes to pandas.ipynb)

generate index.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@
280280
" 11.06 gzip, zipfile, tarfile 模块:处理压缩文件\n",
281281
" 11.07 logging 模块:记录日志\n",
282282
" 11.08 string 模块:字符串处理\n",
283+
" 11.09 collections 模块:更多数据结构\n",
284+
" 11.10 requests 模块:HTTP for Human\n",
283285
"12. Pandas\n",
284286
" 12.01 十分钟上手 Pandas\n"
285287
]

index.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@
246246
"\t - [11.06 gzip, zipfile, tarfile 模块:处理压缩文件](11. useful tools/11.06 gzip, zipfile, tarfile.ipynb)\n",
247247
"\t - [11.07 logging 模块:记录日志](11. useful tools/11.07 logging.ipynb)\n",
248248
"\t - [11.08 string 模块:字符串处理](11. useful tools/11.08 string.ipynb)\n",
249+
"\t - [11.09 collections 模块:更多数据结构](11. useful tools/11.09 collections.ipynb)\n",
250+
"\t - [11.10 requests 模块:HTTP for Human](11. useful tools/11.10 requests.ipynb)\n",
249251
"- [12. **Pandas**](12. pandas)\n",
250252
"\t - [12.01 十分钟上手 Pandas](12. pandas/12.01 ten minutes to pandas.ipynb)\n"
251253
]

index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,7 @@
144144
- [11.06 gzip, zipfile, tarfile 模块:处理压缩文件](11. useful tools/11.06 gzip, zipfile, tarfile.ipynb)
145145
- [11.07 logging 模块:记录日志](11. useful tools/11.07 logging.ipynb)
146146
- [11.08 string 模块:字符串处理](11. useful tools/11.08 string.ipynb)
147+
- [11.09 collections 模块:更多数据结构](11. useful tools/11.09 collections.ipynb)
148+
- [11.10 requests 模块:HTTP for Human](11. useful tools/11.10 requests.ipynb)
147149
- [12. **Pandas**](12. pandas)
148150
- [12.01 十分钟上手 Pandas](12. pandas/12.01 ten minutes to pandas.ipynb)

0 commit comments

Comments
 (0)