understanding htmllib

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Bear

    #1

    understanding htmllib

    I'm trying to understand how to use the HTMLParser in htmllib but I'm not
    seeing enough examples.

    I just want to grab the contents of everything enclosed in a '<body>' tag,
    i.e. items from where <bodybegins to where </bodyends. I start by doing

    class HTMLBody(HTMLPa rser):
    def __init__(self):
    self.contents = []

    def handle_starttag ()..

    Now I'm stuck. I cant see that there is a method on handle_starttag that
    would return everthing to the end tag. And I haven't seen anything on how
    to define my one handle_unknownt ag..

    Any pointers would be greatly appreciated. The documentation on this module
    at python.org seems to assume a great deal about what the reader would
    already know about which methods they should subclass.

    --
    David Bear
    -- let me buy your intellectual property, I want to own your thoughts --
  • Fredrik Lundh

    #2
    Re: understanding htmllib

    David Bear wrote:
    I'm trying to understand how to use the HTMLParser in htmllib but I'm not
    seeing enough examples.
    >
    I just want to grab the contents of everything enclosed in a '<body>' tag,
    i.e. items from where <bodybegins to where </bodyends. I start by doing
    >
    class HTMLBody(HTMLPa rser):
    def __init__(self):
    self.contents = []
    >
    def handle_starttag ()..
    >
    Now I'm stuck. I cant see that there is a method on handle_starttag that
    would return everthing to the end tag. And I haven't seen anything on how
    to define my one handle_unknownt ag..
    htmllib is designed to be used together with a formatting object. if
    you just want to work with tags, use sgmllib instead. some variation of
    the SGMLFilter example on this page might be what you need:



    if you want a DOM-like structure instead of an event stream, use



    usage:
    >>import BeautifulSoup as BS
    >>soup = BS.BeautifulSou p(open("page.ht ml"))
    >>str(soup.body )
    '<body>\n<h1>Bo dy Title</h1>\n<p>Paragra ph</p>\n</body>'
    >>soup.body.ren derContents()
    '\n<h1>Body Title</h1>\n<p>Paragra ph</p>\n'

    </F>

    Comment

    Working...