Three styles of Python Web download not completed

Source: Internet
Author: User

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 ()) # formatting string {BASE_URL}/{CC}/{CC}
Print (URL)
resp = requests.get (URL) #以二进制形式下载
return resp.content# binary data


Def show (text):
Print (text,end= ")
Sys.stdout.flush () #在liunx系统下, without this sentence, the printing time is not immediately printed. If this problem does not exist in Windows (it is normal to encounter line breaks to refresh the stdout buffer)

def download_many (cc_list):
For CC in sorted (cc_list): #sorted () What is the result and function of this sort?
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
# Flags Download in 8.57s






#concurrent. Futures module Download
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)) # Sets the maximum number of threads

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
# Flags Download in 2.44s
if __name__ = = ' __main__ ':
Main_n (Download_many)

























































Three styles of Python Web download not completed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.