Skip to content

Instantly share code, notes, and snippets.

@shashi
Created August 16, 2009 08:55
Show Gist options
  • Save shashi/168590 to your computer and use it in GitHub Desktop.
Save shashi/168590 to your computer and use it in GitHub Desktop.
# Script to get all xkcd comic pics
import urllib2
tits={}
def zeropad(i):
plen=3-len(str(i))
pstr=''
j=0
while(j<plen):
pstr=pstr+'0'
j+=1
return pstr+str(i)
for i in range(274,700):
print str(i)
try:
dat=urllib2.urlopen('http://xkcd.com/'+str(i)+'/').read()
except:
print "Couldn't get it.."
continue
try:
url=dat.split("""<br/>
<br/>
<img src=\"""")[1].split('" title="')
title=url[1].split('"')[0]
url=url[0]
print " "+url+" : "+title
tits[zeropad(i)]=title
f=open('/home/shashi/xkcd/'+zeropad(i)+'-'+
url.replace('http://imgs.xkcd.com/comics/',''),"w")
except:
print "Error parsing html"
try:
f.write(urllib2.urlopen(url).read())
except:
print "Error downloading pic."
f.close()
f=open("xkcd.lst","w")
f.write(str(tits))
f.close()
print "Done, yo! Enjoy!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment