@@ -0,0 +1,1580 @@
+#! python3%0A# P07_multidownloadXkcd.py - Downloads XKCD comics using multiple threads.%0A%0Aimport requests, os, bs4, threading%0Aos.makedirs('xkcd', exist_ok=True)  # store comics in ./xkcd%0A%0A%0Adef downloadXkcd(startComic, endComic):%0A    for urlNumber in range(startComic, endComic):%0A        # Download the page.%0A        print('Downloading page http://xkcd.com/%25s...' %25 (urlNumber))%0A        res = requests.get('http://xkcd.com/%25s' %25 (urlNumber))%0A        res.raise_for_status()%0A%0A        soup = bs4.BeautifulSoup(res.text)%0A%0A        # Find the URL of the comic image.%0A        comicElem = soup.select('#comic img')%0A        if comicElem == %5B%5D:%0A            print('Could not find comic image.')%0A        else:%0A            comicUrl = comicElem%5B0%5D.get('src')%0A            # Download the image.%0A            print('Downloading image %25s...' %25 (comicUrl))%0A            res = requests.get(comicUrl)%0A            res.raise_for_status()%0A%0A            # Save the image to ./xkcd.%0A            imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')%0A            for chunk in res.iter_content(100000):%0A                imageFile.write(chunk)%0A            imageFile.close()%0A%0A%0A# Create and start the Thread objects.%0AdownloadThreads = %5B%5D  # a list of all the Thread objects%0Afor i in range(0, 1400, 100):  # loops 14 times, creates 14 threads%0A    downloadThread = threading.Thread(target=downloadXkcd, args=(i, i + 99))%0A    downloadThreads.append(downloadThread)%0A    downloadThread.start()%0A%0A# Wait for all threads to end.%0Afor downloadThread in downloadThreads:%0A    downloadThread.join()%0Aprint('Done.')%0A

Initialize P07_multidownloadXkcd