getting a KeyError:'href' any ideas?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • homepricemaps@gmail.com

    #1

    getting a KeyError:'href' any ideas?

    i have an
    href which looks like this:


    <td class="all">
    <a class="btn" name="D1" href="http://www.cnn.com">
    </a>


    here is my code

    for incident in row('td', {'class':'all'} ):
    n = incident.findNe xtSibling('a', {'class': 'btn'})
    link = incident.findNe xtSibling['href'] + "','"



    and the full error is this:


    File "/home/felafela/BeautifulSoup.p y", line 301, in __getitem__
    return self._getAttrMa p()[key]
    KeyError: 'href'


    any idea what i'm doing wrong here with the syntax? thanks in advance

  • Mike Meyer

    #2
    Re: getting a KeyError:'href' any ideas?

    "homepricemaps@ gmail.com" <homepricemaps@ gmail.com> writes:

    Please use less whitespace in your posts in the future. There's really
    no need to put two blank lines between sections.
    [color=blue]
    > i have an
    > href which looks like this:
    > <td class="all">
    > <a class="btn" name="D1" href="http://www.cnn.com">
    > </a>
    > here is my code
    > for incident in row('td', {'class':'all'} ):
    > n = incident.findNe xtSibling('a', {'class': 'btn'})
    > link = incident.findNe xtSibling['href'] + "','"
    > any idea what i'm doing wrong here with the syntax? thanks in advance[/color]

    It's not the syntax, it's the logic. the a element is not a sibling of
    the td element, it's a child. findNextSibling is going to return the
    next td, assuming there is one. Trying to index
    incident.findNe xtSibling in the next line is also broken. That's a
    method, not an indexable object. So that line will also break.

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

    Comment

    • LordLaraby

      #3
      Re: getting a KeyError:'href' any ideas?

      You wrote:[color=blue]
      > i have an
      > href which looks like this:
      > <td class="all">
      > <a class="btn" name="D1" href="http://www.cnn.com">
      > </a>
      > here is my code
      > for incident in row('td', {'class':'all'} ):
      > n = incident.findNe xtSibling('a', {'class': 'btn'})
      > link = incident.findNe xtSibling['href'] + "','"
      > any idea what i'm doing wrong here with the syntax? thanks in advance[/color]
      Since you already found the anchor tag with the 'btn' class attribute,
      just grab it's href attribute like so:
      for incident in row('td', {'class':'all'} ):
      n = incident.findNe xtSibling('a', {'class': 'btn'})
      link = n['href'] + "','"

      Works for me...

      LL

      Comment

      • homepricemaps@gmail.com

        #4
        Re: getting a KeyError:'href' any ideas?

        what is the syntax used to find a child of td?

        Mike Meyer wrote:[color=blue]
        > "homepricemaps@ gmail.com" <homepricemaps@ gmail.com> writes:
        >
        > Please use less whitespace in your posts in the future. There's really
        > no need to put two blank lines between sections.
        >[color=green]
        > > i have an
        > > href which looks like this:
        > > <td class="all">
        > > <a class="btn" name="D1" href="http://www.cnn.com">
        > > </a>
        > > here is my code
        > > for incident in row('td', {'class':'all'} ):
        > > n = incident.findNe xtSibling('a', {'class': 'btn'})
        > > link = incident.findNe xtSibling['href'] + "','"
        > > any idea what i'm doing wrong here with the syntax? thanks in advance[/color]
        >
        > It's not the syntax, it's the logic. the a element is not a sibling of
        > the td element, it's a child. findNextSibling is going to return the
        > next td, assuming there is one. Trying to index
        > incident.findNe xtSibling in the next line is also broken. That's a
        > method, not an indexable object. So that line will also break.
        >
        > <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

        • homepricemaps@gmail.com

          #5
          Re: getting a KeyError:'href' any ideas?

          ok the syntax for next is this:

          b = n.findNext



          LordLaraby wrote:[color=blue]
          > You wrote:[color=green]
          > > i have an
          > > href which looks like this:
          > > <td class="all">
          > > <a class="btn" name="D1" href="http://www.cnn.com">
          > > </a>
          > > here is my code
          > > for incident in row('td', {'class':'all'} ):
          > > n = incident.findNe xtSibling('a', {'class': 'btn'})
          > > link = incident.findNe xtSibling['href'] + "','"
          > > any idea what i'm doing wrong here with the syntax? thanks in advance[/color]
          > Since you already found the anchor tag with the 'btn' class attribute,
          > just grab it's href attribute like so:
          > for incident in row('td', {'class':'all'} ):
          > n = incident.findNe xtSibling('a', {'class': 'btn'})
          > link = n['href'] + "','"
          >
          > Works for me...
          >
          > LL[/color]

          Comment

          Working...