Python Multi-threaded download page pictures and save to a specific directory

Source: Internet
Author: User

#!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

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.