htmllib & do_meta question

Martin v. Loewis martin at v.loewis.de
Fri May 3 01:25:21 EDT 2002


Marcus Schopen <marcus.schopen at uni-bielefeld.de> writes:

> I use this for parsing a HTML File for META TAGS description and
> keywords. It seems to work fine. Did I miss something or is there a
> better way to do this?

That is a good way to do this.

>     def do_meta(self, attributes):
>         name = content = ""
>         for key, value in attributes:
>             if key == "name":
>                 name = value
>             elif key == "content":
>                 content = value
>         if string.lower(name) == "description":
>             self.description = string.strip(content)
>         elif string.lower(name) == "keywords":
>             self.keywords = string.strip(content)

If you want to further dispatch on the name, you could do

  def do_meta(self, attributes):
    a_dict = dict(attributes)
    name = a_dict.get('name')
    if name:
      try:
        m = getattr(self, 'meta_'+name)
      except AttributeError:
        return self.meta_name(name, a_dict['content'])
      else:
        return m(a_dict['content'])
    http_equiv = a_dict.get('http-equiv')
    if http_equiv:
      return self.meta_http_equiv(http_equiv, a_dict['content'])

Notice that the builtin dict won't give you a case-insensitive
dictionary, so you'll have to do all the lowercasing explicitly,
anyway.

HTH,
Martin



More information about the Python-list mailing list