Moved away from deprecated SGMLlib parser

I'd heard that the SGMLlib parser had been deprecated in favor of
HTMLParser, so now I've moved to that.
This commit is contained in:
Tom Willemsen 2011-06-11 11:38:21 +02:00
parent 4bd89ebded
commit 02524e6780

View file

@ -17,33 +17,35 @@
# along with 4grab. If not, see <http://www.gnu.org/licenses/>. # along with 4grab. If not, see <http://www.gnu.org/licenses/>.
###################################################################### ######################################################################
import sgmllib from HTMLParser import HTMLParser, HTMLParseError
import re import re
class MyParser(sgmllib.SGMLParser): class MyParser(HTMLParser):
def __init__(self, verbose=0): def __init__(self):
sgmllib.SGMLParser.__init__(self, verbose) HTMLParser.__init__(self)
self.hyperlinks = [] self.hyperlinks = []
self.url_reg = re.compile('res/\d+\Z') self.url_reg = re.compile('res/\d+\Z')
self.prev = ""
def parse(self, s): def parse(self, s):
self.feed(s) self.feed(s)
self.close() self.close()
def start_a(self, attributes): def handle_starttag(self, tag, attrs):
for name, value in attributes: prev = ""
if name == "href":
if tag == 'a':
for name, value in attrs:
if name == 'href':
if self.url_reg.search(value) != None: if self.url_reg.search(value) != None:
if self.prev != value: if prev != value:
self.hyperlinks.append(value) self.hyperlinks.append(value)
self.prev = value prev = value
def get_hyperlinks(self): def get_hyperlinks(self):
return self.hyperlinks return self.hyperlinks
class MySubParser(MyParser): class MySubParser(MyParser):
def __init__(self, verbose=0): def __init__(self):
MyParser.__init__(self, verbose) MyParser.__init__(self)
self.url_reg = re.compile('/src/\d+\.\w{3,4}\Z') self.url_reg = re.compile('/src/\d+\.\w{3,4}\Z')