You are not logged in.
Pages: 1
Python 2.x Code
"""
Downloads all XKCD comic strips with respective titles (except 404, guess why)
Author: Manan
Version 2172014
"""
#!/usr/bin/env python
import urllib
import os.path
import sys
import re
def download():
directory = raw_input("Enter the filepath for the comics to be saved ")
count = 0
if not directory.endswith("/"):
directory += str("/")
pattern = re.compile("/comics/[a-z0-9_()]*.(jpg|png|gif)")
while True:
count += 1
if count == 404:
# Comic number 404 is not supported.
count +=1
page = urllib.urlopen(str("http://www.xkcd.com/") + str(count))
if "Last-Modified" not in page.headers:
break
search = pattern.search(page.read())
if search:
matcher = search.group()
name = os.path.basename(matcher)
if (not os.path.exists(str(directory) + str(name))):
urllib.urlretrieve("http://imgs.xkcd.com/" + matcher, os.path.join(directory, name))
print "Downloaded comic " + str(count) + " successfully"
else:
print "Comic was already present in the specified directory"
if __name__ == '__main__':
download()
'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.
Offline
Pages: 1