-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathxkcd_thread.py
36 lines (31 loc) · 1.31 KB
/
xkcd_thread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /usr/bin/python3
# multidownloadXkcd.py - Downloads XKCD comics using multiple threads.
import requests, os,threading
from bs4 import BeautifulSoup
os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd
def downloadXkcd(startComic, endComic):
for urlNumber in range(startComic, endComic):
print('Downloading page http://xkcd.com/%s...' % (urlNumber))
res = requests.get('http://xkcd.com/%s' % (urlNumber))
soup = BeautifulSoup(res.text,'lxml')
# Find the URL of the comic image.
comicElem = soup.select('#comic img')
if comicElem == []:
print('Could not find comic image.')
else:
comicUrl = 'http:'+comicElem[0].get('src')
# Download the image.
print('Downloading image %s...' % (comicUrl))
res = requests.get(comicUrl)
# Save the image to ./xkcd.
imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
imageFile.write(res.content)
imageFile.close()
downloadThreads = []
for i in range(1, 1648, 100):
downloadThread = threading.Thread(target=downloadXkcd, args=(i, min(i + 100,1649)))
downloadThreads.append(downloadThread)
downloadThread.start()
for downloadThread in downloadThreads:
downloadThread.join()
print('Done.')