问题
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!
回答1:
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"])
回答2:
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
来源:https://stackoverflow.com/questions/19559191/getting-tag-names-with-beautifulsoup