Ai
2 Star 0 Fork 1

jackfrued/python2004

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
example16.py 1010 Bytes
一键复制 编辑 原始数据 按行查看 历史
jackfrued 提交于 2020-11-26 11:49 +08:00 . 多线程下载
import time
from concurrent.futures.thread import ThreadPoolExecutor
import requests
def download_picture(pic_url):
resp = requests.get(pic_url)
if resp.status_code == 200:
filename = pic_url[pic_url.rfind('/') + 1:]
with open(f'images/{filename}', 'wb') as file:
file.write(resp.content)
def main():
pool = ThreadPoolExecutor(max_workers=32)
futures = []
key = 'e8c5524dd2a365f20908ced735f8e480'
start = time.time()
for page in range(1, 11):
resp = requests.get(
url=f'http://api.tianapi.com/meinv/index?key={key}&num=20&page={page}'
)
result = resp.json()
if result['code'] == 200:
for beauty in result['newslist']:
f = pool.submit(download_picture, beauty['picUrl'])
futures.append(f)
for f in futures:
f.result()
end = time.time()
print(f'下载时间:{end - start:.3f}秒')
pool.shutdown()
if __name__ == '__main__':
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/jackfrued/python2004.git
git@gitee.com:jackfrued/python2004.git
jackfrued
python2004
python2004
master

搜索帮助