標籤: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 網路下載的三種風格 未完成