Repository navigation
Expand file tree
/
Copy path4-python-request.html
More file actions
594 lines (270 loc) · 20.3 KB
/
Copy path4-python-request.html
File metadata and controls
594 lines (270 loc) · 20.3 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>2.4 Python 请求 · GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="5-python-syntax-advanced.html" />
<link rel="prev" href="3-python-syntax.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
介绍
</a>
</li>
<li class="chapter " data-level="1.2" data-path="../1.Kickoff/">
<a href="../1.Kickoff/">
KickOff Party
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="../1.Kickoff/1-teaching-goals.html">
<a href="../1.Kickoff/1-teaching-goals.html">
1.1 教学目的
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="../1.Kickoff/2-software-tool.html">
<a href="../1.Kickoff/2-software-tool.html">
1.2 软件和工具
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="../1.Kickoff/3-pre-homework.html">
<a href="../1.Kickoff/3-pre-homework.html">
1.3 课前自学
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="./">
<a href="./">
Python 简介
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="1-data-engineering-intro.html">
<a href="1-data-engineering-intro.html">
2.1 数据工程介绍
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="2-python.html">
<a href="2-python.html">
2.2 Python 介绍
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="3-python-syntax.html">
<a href="3-python-syntax.html">
2.3 Python 语法
</a>
</li>
<li class="chapter active" data-level="1.3.4" data-path="4-python-request.html">
<a href="4-python-request.html">
2.4 Python 请求
</a>
</li>
<li class="chapter " data-level="1.3.5" data-path="5-python-syntax-advanced.html">
<a href="5-python-syntax-advanced.html">
2.5 Python 语法进阶
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../3.Crawler/">
<a href="../3.Crawler/">
爬虫
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../3.Crawler/1-python-html.html">
<a href="../3.Crawler/1-python-html.html">
3.1 HTML
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../3.Crawler/2-full-site-crawler.html">
<a href="../3.Crawler/2-full-site-crawler.html">
3.2 爬虫实战
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="../3.Crawler/3-python-json.html">
<a href="../3.Crawler/3-python-json.html">
3.3 选学:JSON 简介
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="../3.Crawler/4-login-and-js-render.html">
<a href="../3.Crawler/4-login-and-js-render.html">
3.4 选学:爬虫的难点
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../4.Processing/">
<a href="../4.Processing/">
数据处理
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../4.Processing/1-data-processing.html">
<a href="../4.Processing/1-data-processing.html">
4.1 初见数据处理
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../4.Processing/2-data-practice.html">
<a href="../4.Processing/2-data-practice.html">
4.2 数据处理应用实例
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../4.Processing/3-pandas.html">
<a href="../4.Processing/3-pandas.html">
4.3 选学:Pandas
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.6" data-path="../demo.html">
<a href="../demo.html">
Demo练习
</a>
</li>
<li class="chapter " data-level="1.7" data-path="../project.html">
<a href="../project.html">
大作业
</a>
</li>
<li class="chapter " data-level="1.8" data-path="../sharing-and-learning-suggestions.html">
<a href="../sharing-and-learning-suggestions.html">
分享与后续学习建议
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >2.4 Python 请求</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="python-请求">Python 请求</h1>
<blockquote>
<p>Cooking time: 15 mins active / 20 mins passive</p>
<p><strong>Tasks For Instructors:</strong> 请讲师简单介绍 Python 发请求,HTTP知识。<br><strong> 本节稍微了解一下便可 </strong></p>
</blockquote>
<h2 id="http-简介">HTTP 简介</h2>
<p>HTTP 是超文本传输协议(hypertext transfer protocol)的缩写。</p>
<p>(常见语病:「HTTP 协议」。「P」和「协议」同义重复了,只用「HTTP」即可)</p>
<p>HTTP 规定了客户端和服务器之间传输网页和文件的办法。</p>
<p>客户端:例如浏览器、爬虫、手机应用。
服务器:用网址定位,一般运行于一台电脑上的 80 或者 443 端口。</p>
<p>在 HTTP 语境下,网页或文件一般统称资源(resource)。</p>
<p>HTTP 请求由以下部分组成:</p>
<ul>
<li>请求行,包含请求动作(verb)和请求路径,HTTP 的版本,例如 <code>GET /images/logo.gif HTTP/1.1</code></li>
<li>多个请求头,例如告诉服务器我希望获得中文的响应 <code>accept-language: zh</code></li>
<li>一个空行</li>
<li>可选的消息体 (body)</li>
</ul>
<p>常用的 HTTP 请求动作(verb)有</p>
<ul>
<li><code>GET</code> 一般用来获取一个资源,例如 <code>GET /images/logo.gif HTTP/1.1</code>。</li>
<li><code>POST</code> 一般用来提交一个资源,常用于表单提交或登录。</li>
</ul>
<p>(verb 的使用只是一种惯例,也有网站不遵守的)</p>
<p>HTTP 响应由以下部分组成:</p>
<ul>
<li>响应行,包含 HTTP 版本和响应代码,例如 <code>HTTP/2 200</code></li>
<li>多个响应头,例如告诉客户端消息体的格式 <code>content-type: image/gif</code></li>
<li>一个空行</li>
<li>可选的消息体</li>
</ul>
<p>常见的响应代码</p>
<ul>
<li>2XX 正常工作,如 200 OK, 201 Created</li>
<li>3XX 重定向,如 302 跳转,304 网页没变化</li>
<li>4XX 客户端出问题了,如 400 非法请求,404 资源没找到</li>
<li>5XX 服务器出问题了,如 500 服务器内部错误, 504 后台服务超时</li>
</ul>
<h2 id="使用-chrome-开发者工具查看请求">使用 Chrome 开发者工具查看请求</h2>
<p>打开菜单:视图 -> 开发者 -> 开发者工具,选择 "网络" 栏,刷新页面</p>
<p><img src="inspect.png" alt=""></p>
<h2 id="requests-的使用">Requests 的使用</h2>
<p><a href="http://docs.python-requests.org/en/master/" target="_blank">Requests</a> 是一个方便的 HTTP 请求库。</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
response = requests.get(<span class="hljs-string">'https://c.xkcd.com/random/comic/'</span>)
print(response.text) <span class="hljs-comment"># 打印网页的 HTML 源代码</span>
</code></pre>
<p>可以看到以 <code><!DOCTYPE html></code> 开头的 HTML 内容,下面我们先讲解一下 Python 进阶语法,然后再介绍如何从 HTML 中提取信息。</p>
<p>如果请求的是一张图片,图片文件往往包含大量的非字符数据,需要用 <code>response.content</code> 获得内容</p>
<pre><code class="lang-python">response = requests.get(<span class="hljs-string">'https://ws4.sinaimg.cn/large/006tNbRwly1fwict5oyqdj31kw1kw124.jpg'</span>)
<span class="hljs-keyword">with</span> open(<span class="hljs-string">"crawled_image.jpg"</span>, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> f:
f.write(response.content)
</code></pre>
<p>因为写入的是二进制数据,这里的 <code>open</code> 使用了 <code>'rb'</code> 参数表明用二进制输出流写文件。</p>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="3-python-syntax.html" class="navigation navigation-prev " aria-label="Previous page: 2.3 Python 语法">
<i class="fa fa-angle-left"></i>
</a>
<a href="5-python-syntax-advanced.html" class="navigation navigation-next " aria-label="Next page: 2.5 Python 语法进阶">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"2.4 Python 请求","level":"1.3.4","depth":2,"next":{"title":"2.5 Python 语法进阶","level":"1.3.5","depth":2,"path":"2.Python/5-python-syntax-advanced.md","ref":"2.Python/5-python-syntax-advanced.md","articles":[]},"previous":{"title":"2.3 Python 语法","level":"1.3.3","depth":2,"path":"2.Python/3-python-syntax.md","ref":"2.Python/3-python-syntax.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"2.Python/4-python-request.md","mtime":"2018-10-30T03:15:01.077Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-10-30T04:36:00.086Z"},"basePath":"..","book":{"language":""}});
});
</script>
</div>
<script src="../gitbook/gitbook.js"></script>
<script src="../gitbook/theme.js"></script>
<script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="../gitbook/gitbook-plugin-search/search.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>