Skip to main content
 首页 » 编程设计

python之获取标签列表,获取BeautifulSoup中的属性值

2025年05月04日11开发

我正在尝试使用 BeautifulSoup 所以得到一个 HTML 列表 <div>标签,然后检查它们是否具有名称属性,然后返回该属性值。请看我的代码:

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 

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

请您参考如下方法:

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

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