Reputation: 69
i'm very new to python so first of all sorry, i want to print href content from a selection made with find() method from beautifulsoup but i cannot do that and i don't know why. i've do this
from bs4 import BeautifulSoup
from requests import session
payload = {
'btnSubmit': 'Login',
'username': 'xxx',
'password': 'xxx'
}
with session() as c:
c.post('http://www.xxx.xxx/login.php', data=payload)
request = c.get('http://www.xxx.xxx/xxxx')
soup=BeautifulSoup(request.content)
row_int=soup.find('td',attrs={'class' : 'rnr-cc rnr-bc rnr-icons'})
print row_int['href']
but i've this error
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
execfile ('C:\Users\Francesco\Desktop\prova.py')
File "C:\Users\Francesco\Desktop\prova.py", line 15, in <module>
print row_int['href']
File "C:\Python27\lib\site-packages\bs4\element.py", line 905, in __getitem__
return self.attrs[key]
KeyError: 'href'
the content of row_int is like this:
[<a class="rnr-button-img" data-icon="view" href="xxxxxxx" id="viewLink12" name="viewLink12" title="Details"></a>, u' ']
where i'm wrong?
Upvotes: 1
Views: 125
Reputation: 473873
You need to get the links (a
elements) from inside the td
tags:
row = soup.find('td', attrs={'class': 'rnr-cc rnr-bc rnr-icons'})
a = row.find('a', href=True)
if a:
print a['href']
Or, shorter with a CSS selector
:
for a in soup.select('td.rnr-cc.rnr-bc.rnr-icons a[href]'):
print a['href']
Upvotes: 2