Sarcasm, math, and language + requests
Here's a tiny script to download all the XKCD comics. Create a directory called comics next to the script and it'll throw them all in and name them accordingly.
Made originally for r/tinycode as a personal challenge. Challenge complete in 572 Bytes (not including requests get).
from requests import get
n = 1
while True:
if n == 404:
n += 1
w = f'https://xkcd.com/{n}/info.0.json'
r = get(w)
if r.status_code == 404:
break
d = r.json()
n = d['num']
t = d['safe_title']
u = d['img']
print(f'{n}: {t}')
if u.endswith('png'):
e = 'png'
if u.endswith('jpg'):
e = 'jpg'
if u.endswith('gif'):
e = 'gif'
x = 'comics/{}-{}.{}'.format(n, t.replace(' ', '_').replace('/', '-'), e)
with open(x, "wb") as f:
c = get(u)
f.write(c.content)
n += 1
Thanks for reading. x
Resources
- Python: https://python.org
- Requests: http://docs.python-requests.org
- XKCD - https://xkcd.com