Skip to content

Commit dd3ee47

Browse files
authoredOct 3, 2022
Merge pull request #867 from Indrahas/Indrahas
Added automation script for downloading xkcd comics
2 parents 11f98e6 + 53c552f commit dd3ee47

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
 

‎xkcd_downloader/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## XKCD Comics downloader
2+
3+
A Python script to download the latest XKCD comics.
4+
5+
To run the script, go to the directory in bash,
6+
7+
1. Run the script
8+
```bash
9+
python3 download_xkcd.py url_to_the_comic
10+
```
11+
12+
2. The comics will be downloaded in the same directory as the script.

‎xkcd_downloader/download_xkcd.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! python3
2+
3+
import requests
4+
import os
5+
import bs4
6+
7+
url = input("The url of the XKCD comics") # starting url
8+
os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd
9+
while not url.endswith('#'):
10+
print('Downloading page %s...' % url)
11+
res = requests.get(url)
12+
res.raise_for_status()
13+
14+
soup = bs4.BeautifulSoup(res.text, 'html.parser')
15+
comicElem = soup.select('#comic img')
16+
if comicElem == []:
17+
print('Could not find comic image.')
18+
else:
19+
comicUrl = 'https:' + comicElem[0].get('src')
20+
# Download the image.
21+
print('Downloading image %s...' % (comicUrl))
22+
res = requests.get(comicUrl)
23+
res.raise_for_status()
24+
imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
25+
for chunk in res.iter_content(100000):
26+
imageFile.write(chunk)
27+
imageFile.close()
28+
29+
# Get the Prev button's url.
30+
prevLink = soup.select('a[rel="prev"]')[0]
31+
url = 'https://xkcd.com' + prevLink.get('href')
32+
33+
print('Done.')

0 commit comments

Comments
 (0)
Please sign in to comment.