-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemes_xkcd.py
More file actions
31 lines (24 loc) · 920 Bytes
/
memes_xkcd.py
File metadata and controls
31 lines (24 loc) · 920 Bytes
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
# pip install requests, bs4
import os, requests, bs4
url = 'https://xkcd.com'
os.makedirs('xkcd_memes', exist_ok=True)
while not url.endswith('#'):
print('Downloading page %s....' % url)
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
comic_elem = soup.select('#comic img')
if comic_elem==[]:
print('Could not find comic image.')
else:
comic_url = 'https:' + comic_elem[0].get('src')
print('Downloading image %s...' % comic_url)
res = requests.get(comic_url)
res.raise_for_status()
image_file = open(os.path.join('xkcd_memes', os.path.basename(comic_url)), 'wb')
for chunk in res.iter_content(100000):
image_file.write(chunk)
image_file.close()
prev_link = soup.select('a[rel="prev"]')[0]
url = 'https://xkcd.com' + prev_link.get('href')
print('Done!')