获取标签列表并获取BeautifulSoup中的属性值

Rut*_*uth 1 python beautifulsoup

我正在尝试使用BeautifulSoup,因此获取HTML <div>标记列表,然后检查它们是否具有name属性,然后返回该属性值.请看我的代码:

soup = BeautifulSoup(html) #assume html contains <div> tags with a name attribute
nameTags = soup.findAll('name') 
for n in nameTags:
    if n.has_key('name'):
       #get the value of the name attribute
Run Code Online (Sandbox Code Playgroud)

我的问题是如何获取name属性的值?

Ram*_*esh 6

使用以下代码,它应该工作

nameTags = soup.findAll('div',{"name":True})
for n in nameTags:
    # Do your processing
Run Code Online (Sandbox Code Playgroud)