码迷,mamicode.com
首页 > 编程语言 > 详细

python 网络下载的三种风格 未完成

时间:2018-07-17 23:29:58      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:sed   des   show   save   load   lis   int   split   img   

import os
import time
import sys

import requests
#依序下载
POP20_CC = (‘CN IN US ID BR PK NG BD RU JP‘
‘MX PH VN ET EG DE IR TE CD TR‘).split()
BASE_URL = ‘http://flupy.org/data/flags‘
DEST_DIR = ‘downloads/‘


def save_flag(img, filename):
path = os.path.join(DEST_DIR, filename)
if not os.path.isdir(DEST_DIR):
os.mkdir(DEST_DIR)

with open(path, ‘wb‘) as fp:
if fp.writable():
fp.write(img)


def get_flag(cc):#下载内容
url = ‘{}/{cc}/{cc}.gif‘.format(BASE_URL, cc=cc.lower()) # 格式化字符串{BASE_URL}/{cc}/{cc}
print(url)
resp = requests.get(url)#以二进制形式下载
return resp.content#返回二进制数据


def show(text):
print(text,end=‘ ‘)
sys.stdout.flush()#在liunx系统下,不加这句,打印时间不是立刻打印的。假如在windows中不存在这个问题(正常情况遇到换行才会刷新stdout缓冲)

def download_many(cc_list):
for cc in sorted(cc_list):#sorted()这个排序的结果和作用是什么?
image=get_flag(cc)
show(cc)

save_flag(image,cc.lower()+‘.gif‘)
return len(cc_list)

def main_n(download_many):
t1=time.time()
count=download_many(POP20_CC)
elapsed=time.time()-t1
msg=‘\n{} flags download in {:.2f}s‘
print(msg.format(count,elapsed))

if __name__ == ‘__main__‘:
main_n(download_many)
#BD BR CD CN DE EG ET ID IN IR JPMX NG PH PK RU TE TR US VN
# 19 flags download in 8.57s






#concurrent.futures 模块下载
from concurrent import futures
import os
import time
import requests
import sys

MAX_WORKERS = 20
POP20_CC = (‘CN IN US ID BR PK NG BD RU JP‘
‘MX PH VN ET EG DE IR TE CD TR‘).split()
BASE_URL = ‘http://flupy.org/data/flags‘
DEST_DIR = ‘downloads/‘


def save_flag(img, filename):
path = os.path.join(DEST_DIR, filename)
if not os.path.isdir(DEST_DIR):
os.mkdir(DEST_DIR)

with open(path, ‘wb‘) as fp:
if fp.writable():
fp.write(img)


def get_flag(cc):
url = ‘{}/{cc}/{cc}.gif‘.format(BASE_URL, cc=cc.lower())
resp = requests.get(url)
return resp.content


def show(text):
print(text, end=‘ ‘)
sys.stdout.flush()


def main_n(download_many):
t1 = time.time()
count = download_many(POP20_CC)
elapsed = time.time() - t1
msg = ‘\n{} flags download in {:.2f}s‘
print(msg.format(count, elapsed))


def download_one(cc):
image = get_flag(cc)
show(cc)
save_flag(image, cc.lower() + ‘.gif‘)
return cc


def download_many(cc_list):
workers = min(MAX_WORKERS, len(cc_list)) # 设置最大线程数

with futures.ThreadPoolExecutor(workers) as executor:
res = executor.map(download_one, sorted(cc_list))
return len(list(res))


# BD BR CN ID EG JPMX IN NG RU TE ET CD IR PH PK VN DE US TR
# 19 flags download in 2.44s
if __name__ == ‘__main__‘:
main_n(download_many)

























































python 网络下载的三种风格 未完成

标签:sed   des   show   save   load   lis   int   split   img   

原文地址:https://www.cnblogs.com/yanxiatingyu/p/9326244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!