HTML Parser - beginner needs help

Niklas Frykholm r2d2 at mao.acc.umu.se
Fri Sep 15 03:06:23 EDT 2000


>Can somebody provide small piece of code, which returns list of  img tags?

My metahtml library (http://www.acc.umu.se/~r2d2/files/python/metahtmllib.html)
simplifies some HTML parsing tasks. For a task as simple as this one the gain
is maybe not that great, but if you still want to try it, the code would be:

---

from metahtmllib import MetaHTMLParser
import sys

class ImgParser(MetaHTMLParser):
        def reset(self):
                MetaHTMLParser.reset(self)
                self.show_warnings(0)
                self.imgs = []

        def img(self, attr):
                self.imgs.append(attr["src"])

        def result(self):
                return self.imgs

print ImgParser().parse(open(sys.argv[1]).read())

---

// Niklas           



More information about the Python-list mailing list