Last active
December 17, 2015 15:09
-
-
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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