Skip to content

Commit 3f74303

Browse files
committed
update Requests.md
1 parent ccc4f46 commit 3f74303

1 file changed

Lines changed: 11 additions & 15 deletions

File tree

HTTP/Requests.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -331,46 +331,42 @@ r.raw.read(10)
331331
>>> import requests
332332

333333
>>> headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
334-
>>> r = requests.get('https://toutiao.io/shares/789751/url', headers=headers)
334+
>>> r = requests.get('https://toutiao.io/k/c32y51', headers=headers)
335335

336336
>>> r.status_code
337337
200
338338

339339
>>> r.url # 发生了重定向,响应对象的 url,跟请求对象不一样
340-
u'https://funhacks.net/2016/12/06/flask_react_news/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io'
340+
u'http://www.jianshu.com/p/490441391db6?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io'
341341

342342
>>> r.history
343-
[<Response [302]>, <Response [302]>]
344-
>>> r.history[0].text
345-
u'<html><body>You are being <a href="https://toutiao.io/k/fsntvg">redirected</a>.</body></html>'
343+
[<Response [302]>]
346344

347-
>>> r.history[1].text
348-
u'<html><body>You are being <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fzj429%2Fexplore-python%2Fcommit%2F%3Cspan+class%3D"x x-first x-last">https://funhacks.net/2016/12/06/flask_react_news/?hmsr=toutiao.io&amp;utm_medium=toutiao.io&amp;utm_source=toutiao.io">redirected</a>.</body></html>'
345+
>>> r.history[0].text
346+
u'<html><body>You are being <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fzj429%2Fexplore-python%2Fcommit%2F%3Cspan+class%3D"x x-first x-last">http://www.jianshu.com/p/490441391db6?hmsr=toutiao.io&amp;utm_medium=toutiao.io&amp;utm_source=toutiao.io">redirected</a>.</body></html>'
349347
```
350348

351-
可以看到,我们访问网址 `https://toutiao.io/shares/789751/url` 被重定向到了下面的链接:
349+
可以看到,我们访问网址 `https://toutiao.io/k/c32y51` 被重定向到了下面的链接:
352350

353351
```python
354-
u'https://funhacks.net/2016/12/06/flask_react_news/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io'
352+
http://www.jianshu.com/p/490441391db6?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io'
355353
```
356354

357-
我们还看到 `r.history` 包含了两个 Response 对象列表,我们可以用它来追踪重定向。
355+
我们还看到 `r.history` 包含了一个 Response 对象列表,我们可以用它来追踪重定向。
358356

359357
如果请求方法是 GET、POST、PUT、OPTIONS、PATCH 或 DELETE,我们可以通过 `all_redirects` 参数禁止重定向:
360358

361359
```python
362360
>>> import requests
363361

364362
>>> headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
365-
>>> r = requests.get('https://toutiao.io/shares/789751/url', headers=headers, allow_redirects=False)
366-
>>> r.status_code
367-
200
363+
>>> r = requests.get('https://toutiao.io/k/c32y51', headers=headers, allow_redirects=False)
368364
>>> r.url # 禁止重定向,响应对象的 url 跟请求对象一致
369-
u'https://toutiao.io/shares/789751/url'
365+
u'https://toutiao.io/k/c32y51'
370366
>>> r.history
371367
[]
372368
>>> r.text
373-
u'<html><body>You are being <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fzj429%2Fexplore-python%2Fcommit%2F%3Cspan+class%3D"x x-first x-last">https://toutiao.io/k/fsntvg">redirected</a>.</body></html>'
369+
u'<html><body>You are being <a href="proxy.php?url=https%3A%2F%2Fgithub.com%2Fzj429%2Fexplore-python%2Fcommit%2F%3Cspan+class%3D"x x-first x-last">http://www.jianshu.com/p/490441391db6?hmsr=toutiao.io&amp;utm_medium=toutiao.io&amp;utm_source=toutiao.io">redirected</a>.</body></html>'
374370
```
375371

376372
# Cookie

0 commit comments

Comments
 (0)