Getting Tag Names with BeautifulSoup

℡╲_俬逩灬. 提交于 2019-12-24 01:56:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!