Skip to content

Commit 0025d86

Browse files
Some Comics for Fun!
1 parent 4b403cf commit 0025d86

File tree

144 files changed

+48
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /usr/bin/python3.5
2+
3+
"""
4+
5+
Author: Samrat Banerjee
6+
Dated: 23/08/2018
7+
Description: Project: Downloads every single XKCD comic
8+
9+
"""
10+
11+
import requests,os,bs4
12+
13+
url='https://xkcd.com' # starting url
14+
os.makedirs('xkcd') # store comics in ./xkcd
15+
while not url.endswith('#'):
16+
# Downloading the page
17+
print('Downloading page %s...'%url)
18+
res=requests.get(url)
19+
res.raise_for_status()
20+
21+
soup=bs4.BeautifulSoup(res.text)
22+
23+
# Find the url of the comic image
24+
comicElem=soup.select('#comic img')
25+
if comicElem==[]:
26+
print('Could not find comic image.')
27+
else:
28+
try:
29+
comicURL='https:' + comicElem[0].get('src')
30+
# Download the image
31+
print("Downloading image %s..."%(comicURL))
32+
res=requests.get(comicURL)
33+
res.raise_for_status()
34+
except requests.exceptions.MissingSchema:
35+
# skip this comic
36+
prevLink=soup.select('a[rel="prev"]')[0]
37+
url='https://xkcd.com' + prevLink.get('href')
38+
continue
39+
# Save the image to ./xkcd
40+
imageFile=open(os.path.join('xkcd',os.path.basename(comicURL)),'wb')
41+
for chunk in res.iter_content(100000):
42+
imageFile.write(chunk)
43+
imageFile.close()
44+
45+
# Get the previous button's URL
46+
prevLink=soup.select('a[rel="prev"]')[0]
47+
url='https://xkcd.com' + prevLink.get('href')
48+
print('Done.')
70 KB
36.7 KB
128 KB
22 KB
42.9 KB
19.3 KB
50.4 KB
49.8 KB
78.8 KB

0 commit comments

Comments
 (0)