File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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.' )
You can’t perform that action at this time.
0 commit comments