CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day14/mmdownloader.py
Views: 729
1
from time import time
2
from threading import Thread
3
4
import requests
5
6
7
class DownloadHanlder(Thread):
8
9
def __init__(self, url):
10
super().__init__()
11
self.url = url
12
13
def run(self):
14
filename = self.url[self.url.rfind('/') + 1:]
15
resp = requests.get(self.url)
16
with open('/Users/Hao/Downloads/' + filename, 'wb') as f:
17
f.write(resp.content)
18
19
20
def main():
21
# 通过requests模块的get函数获取网络资源
22
resp = requests.get(
23
'http://api.tianapi.com/meinv/?key=772a81a51ae5c780251b1f98ea431b84&num=10')
24
# 将服务器返回的JSON格式的数据解析为字典
25
data_model = resp.json()
26
for mm_dict in data_model['newslist']:
27
url = mm_dict['picUrl']
28
# 通过多线程的方式实现图片下载
29
DownloadHanlder(url).start()
30
31
32
if __name__ == '__main__':
33
main()
34
35