Skip to content

Instantly share code, notes, and snippets.

@narate
Last active December 17, 2015 15:09
Show Gist options
  • Save narate/5629671 to your computer and use it in GitHub Desktop.
Save narate/5629671 to your computer and use it in GitHub Desktop.
This file used to download file from http://www.par-anoia.net/assessment/books/coding/Python/ Created by Narate Ketram 23/5/2013
# This file used to download file from http://www.par-anoia.net/assessment/books/coding/Python/
# Created by Narate Ketram
# 23/5/2013
import urllib
from HTMLParser import HTMLParser
url = "http://www.par-anoia.net/assessment/books/coding/Python/"
file_names = []
# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag.lower() == "a":
attrs = dict(attrs)
file_names.append(urllib.unquote(attrs.get('href',"")))
def downloadFile():
c = 0
for name in file_names:
c += 1
print "Download... #",c, "of",len(file_names)
print "from",url + name
f = open(name,'wb')
u = urllib.urlopen(url+name)
f.write(u.read())
f.close()
print "Saved to",name,"\n"
print "Getting url data..."
html_string = urllib.urlopen(url)
parser = MyHTMLParser()
parser.feed(html_string.read())
file_names.pop(0) # Parent Directory
file_names.pop(0) # Reference cards
downloadFile()
print "Done...\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment