BeautifulSoup fetch help

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

    #1

    BeautifulSoup fetch help

    Hi,

    I'm using the BeautifulSoup module and having some trouble processing a
    file. It's not printing what I'm expecting. In the code below, I'm expecting
    cells with only "bgcolor" attributes to be printed, but I'm getting cells
    with other attributes and some without any attributes.

    Any help appreciated. Thanks,
    Ted

    import re
    from BeautifulSoup import BeautifulSoup

    text = open('yahoo.htm l').read()
    soup = BeautifulSoup(t ext)
    tables = soup('table', {'border':re.co mpile('.+')})

    for table in tables:
    cells = table.fetch('td ', {'bgcolor':re.c ompile('.+')})
    for cell in cells:
    print cell
    print "============== =="



  • Mike Meyer

    #2
    Re: BeautifulSoup fetch help

    "ted" <tedPLEASE-NO-SPAM-95050@sbcglobal .net> writes:
    [color=blue]
    > I'm using the BeautifulSoup module and having some trouble processing a
    > file. It's not printing what I'm expecting. In the code below, I'm expecting
    > cells with only "bgcolor" attributes to be printed, but I'm getting cells
    > with other attributes and some without any attributes.[/color]

    BeatifulSoups matching is for any tag with a matching attribute, not
    tags that only match that attribute. That's why you're getting tags
    with other attributes.

    However, you can use a callable as the tag argument to check for what
    you want:

    def findtagswithly( name, attr):
    return (lambda tag: tag.name == name and
    len(tag.attrs) == 1 and
    tag.attrs[0][0] == attr)

    ....

    cells = table.fetch(fin dtagswithonly(' a', 'bgcolor'))


    Or, because I wrote it to check out:

    def findtagswithone attrib(name):
    return lambda tag: tag.name == name and len(tag.attrs) == 1

    ....
    cells = table.fetch(fin dtagswithoneatt rib('a', {bgcolor: re.compile('.+) }))

    I'm not sure why you're getting tags without attributes. If the above
    code does that, post some sample data along with the code.

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • ted

      #3
      Re: BeautifulSoup fetch help

      Thanks Mike, works like a charm.

      -Ted


      "Mike Meyer" <mwm@mired.or g> wrote in message
      news:864q4g8vph .fsf@bhuda.mire d.org...[color=blue]
      > "ted" <tedPLEASE-NO-SPAM-95050@sbcglobal .net> writes:
      >[color=green]
      >> I'm using the BeautifulSoup module and having some trouble processing a
      >> file. It's not printing what I'm expecting. In the code below, I'm
      >> expecting
      >> cells with only "bgcolor" attributes to be printed, but I'm getting cells
      >> with other attributes and some without any attributes.[/color]
      >
      > BeatifulSoups matching is for any tag with a matching attribute, not
      > tags that only match that attribute. That's why you're getting tags
      > with other attributes.
      >
      > However, you can use a callable as the tag argument to check for what
      > you want:
      >
      > def findtagswithly( name, attr):
      > return (lambda tag: tag.name == name and
      > len(tag.attrs) == 1 and
      > tag.attrs[0][0] == attr)
      >
      > ...
      >
      > cells = table.fetch(fin dtagswithonly(' a', 'bgcolor'))
      >
      >
      > Or, because I wrote it to check out:
      >
      > def findtagswithone attrib(name):
      > return lambda tag: tag.name == name and len(tag.attrs) == 1
      >
      > ...
      > cells = table.fetch(fin dtagswithoneatt rib('a', {bgcolor:
      > re.compile('.+) }))
      >
      > I'm not sure why you're getting tags without attributes. If the above
      > code does that, post some sample data along with the code.
      >
      > <mike
      > --
      > Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
      > Independent WWW/Perforce/FreeBSD/Unix consultant, email for more
      > information.[/color]


      Comment

      Working...