grequests如何安装呢?
pip install <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/grequests" title="View all posts in grequests" target="_blank">grequests</a></span>
1
|
pip
install
grequests
|
# -*- coding: utf-8 -*- """ @Time: 2018/1/21 @Author: songhao @微信公众号: zeropython @File: Requests_Gevent.py "" [code lang="<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span>"] import grequests import json # 构建urls urls = ["http://lab.crossincode.com/recite/chap?c={}".format(x) for x in range(1, 11)] # 请求队列,还未发出请求 rs = (grequests.get(u) for u in urls) # 批量发出请求,得到响应的列表resps resps = grequests.map(rs) def get_words(ht): dc = json.loads(ht) print(dc['voca']) for resp in resps: # 解析出单词的信息。 get_words(resp.text)
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
|
# -*- coding: utf-8 -*-
""
"
@Time: 2018/1/21
@Author: songhao
@微信公众号: zeropython
@File: Requests_Gevent.py
"
"
[code lang="
python
"]
import grequests
import json
# 构建urls
urls = ["
http
:
/
/
lab
.
crossincode
.
com
/
recite
/
chap
?
c
=
{
}"
.
format
(
x
)
for
x
in
range
(
1
,
11
)
]
# 请求队列,还未发出请求
rs
=
(
grequests
.
get
(
u
)
for
u
in
urls
)
# 批量发出请求,得到响应的列表resps
resps
=
grequests
.
map
(
rs
)
def
get_words
(
ht
)
:
dc
=
json
.
loads
(
ht
)
print
(
dc
[
'voca'
]
)
for
resp
in
resps
:
# 解析出单词的信息。
get_words
(
resp
.
text
)
|
查看结果: