getting a KeyError:'href' any ideas?

H

homepricemaps

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.findNextSibling('a', {'class': 'btn'})
link = incident.findNextSibling['href'] + "','"



and the full error is this:


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


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

Mike Meyer

Please use less whitespace in your posts in the future. There's really
no need to put two blank lines between sections.
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.findNextSibling('a', {'class': 'btn'})
link = incident.findNextSibling['href'] + "','"
any idea what i'm doing wrong here with the syntax? thanks in advance

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.findNextSibling in the next line is also broken. That's a
method, not an indexable object. So that line will also break.

<mike
 
L

LordLaraby

You said:
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.findNextSibling('a', {'class': 'btn'})
link = incident.findNextSibling['href'] + "','"
any idea what i'm doing wrong here with the syntax? thanks in advance
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.findNextSibling('a', {'class': 'btn'})
link = n['href'] + "','"

Works for me...

LL
 
H

homepricemaps

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

Mike said:
Please use less whitespace in your posts in the future. There's really
no need to put two blank lines between sections.
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.findNextSibling('a', {'class': 'btn'})
link = incident.findNextSibling['href'] + "','"
any idea what i'm doing wrong here with the syntax? thanks in advance

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.findNextSibling in the next line is also broken. That's a
method, not an indexable object. So that line will also break.

<mike
 
H

homepricemaps

ok the syntax for next is this:

b = n.findNext


You said:
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.findNextSibling('a', {'class': 'btn'})
link = incident.findNextSibling['href'] + "','"
any idea what i'm doing wrong here with the syntax? thanks in advance
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.findNextSibling('a', {'class': 'btn'})
link = n['href'] + "','"

Works for me...

LL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,852
Latest member
CarlaDowle

Latest Threads

Top