3

I need take "href" attribute from a link (a) tag.

I run

 label_tag = row.find(class_='Label')
 print(label_tag)

and I get (sorry, I can't show link and text for privacy reasons)

<a class="Label" href="_link_">_text_</a>

of type

<class 'bs4.element.Tag'>

but when I run (as shown at BeautifulSoup getting href )

tag_link = label_tag['href']
print(tag_link)

I guess the following error (on first command)

TypeError: 'NoneType' object is not subscriptable

Any clue? Thanks in advance

[SOLVED] EDIT: I was making a mistake (looping over elements with heterogeneous structure)

Community
  • 1
  • 1
dragonmnl
  • 14,578
  • 33
  • 84
  • 129

2 Answers2

7

My guess is that label_tag isn't actually returning the part of the soup you are looking for. This minimal example works:

import bs4
text = '''<a class="Label" href="_link_">_text_</a>'''
soup = bs4.BeautifulSoup(text)
link = soup.find("a",{"class":"Label"})
print (link["href"])

Output:

_link_
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • you are absolutely right. I was making a mistake (looping over elements with heterogeneous structure). now it works. thanks anyway – dragonmnl Nov 25 '14 at 15:48
-1

beacuse in <class 'bs4.element.Tag'>, there is no class of "Label", so label_tag['href'] return None, than an error is occured.

you can use to following code to handle this exception:

if tag_link = label_tag.get('href'):
    print(tag_link)
else:
    print("there is no class of 'Label' or no attribute of 'href'! ")

this method can use to handle exception to prevent program crash.

if your page element is fixed, the former answer is feasible.