python 網路下載的三種風格 未完成

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.