#!python3#multidownloadxkcd.py-download XKCD Comics using multiple Threads.import requestsimport bs4import osimport thr eading# os.mkdir (' xkcd ', exist_ok=true) # Store comics in./xkcdif os.path.exists (' xkcd '): Print ("XKCD is existed!" ) Else:os.mkdir (' xkcd ') def downloadxkcd (Startcomic, endcomic): For Urlnumber in range (Startcomic, endcomic): #Download the page print ("Downloading page http://xkcd.com/%s ..."% urlnumber) res = Requests.get (' http://xk cd.com/%s '% urlnumber) res.raise_for_status () print (res.text) soup = bs4. BeautifulSoup (Res.text) #Find The URL of the comic image. Comicelem = Soup.select (' #comic img ') if Comicelem = = []: print (' Could not find comic images. ') Else:comicurl = Comicelem[0].get (' src ') # #Download the image. # print (' Downloading image%s ... '% (comicurl)) # res = Requests.get (comicurl) # res.raise_for_s Tatus () # # # Save the image to./XKCD # imagefile = open (Os.path.join (' xkcd ', Os.path.basename (Comicu RL), ' WB ') # for Chunk in Res.iter_content (100000): # Imagefile.write (chunk) # image File.close () Downloadthread = Threading. Thread (TARGET=DOWNLOADXKCD (555, 557)) Downloadthread.start () # # Todo:create and start the thread objects# Downloadthreads = [] # A list of all the Thread objects# for I in range ($, ten): # Downloadthread = Thread Ing. Thread (TARGET=DOWNLOADXKCD, args= (i, i+9)) # downloadthreads.append (downloadthread) # Downloadthread.start () # # # TOD O:wait for all threads to end# for Downloadthread in downloadthreads:# downloadthread.join () # Print ("Done.")
Python Multi-threaded download page pictures and save to a specific directory