vjgaero
vjgaero

Reputation: 57

Getting Tag Names with BeautifulSoup

from bs4 import BeautifulSoup
source_code = """<a href="#" name="linkName">ok</a>"""
soup = BeautifulSoup(source_code)
print soup.a.? #find the object name

Using the code displayed above, i am trying to print the anchor tags 'name' which is linkName but i'm not sure which module or object i will be using, i have tried contents,name and tag_name_re.

Can anybody help me out? thanks!

Upvotes: 2

Views: 8676

Answers (2)

Vicky
Vicky

Reputation: 5136

from bs4 import BeautifulSoup
source_code = """<a href="#" name="linkName"><i><b>ok</b></i></a>"""
soup = BeautifulSoup(source_code)
for tag in soup.findChildren():
    print tag.name

findChildren() function will return a list which possess all the children tag.

[<a href="#" name="linkName"><i><b>ok</b></i></a>, <i><b>ok</b></i>, <b>ok</b>]

Then iterate the list to get each tag name.

  Output
    a
    i
    b

Upvotes: 0

user1907906
user1907906

Reputation:

You already answered your question.

soup.a['name']

Edit

If you have more than one a element, you can do this:

x = """<x><a name="foo"/><a name="bar"/></x>"""
s = bs4.BeautifulSoup(x)
for a in s.findChildren("a"):
    print(a["name"])

Upvotes: 6

Related Questions