使用BeautifulSoup获取标签名称

问题描述:

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

使用上面显示的代码,我试图打印锚标记'name',它是linkName,但是我不确定我将使用哪个模块或对象,我已经尝试过contentsnametag_name_re.

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!

您已经回答了问题.

soup.a['name']

修改

如果您有多个a元素,则可以执行以下操作:

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"])