获取标签列表并在BeautifulSoup中获取属性值 [英] Get a list of tags and get the attribute values in BeautifulSoup
本文介绍了获取标签列表并在BeautifulSoup中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正尝试使用BeautifulSoup,因此获取HTML <div>
标记的列表,然后检查它们是否具有名称属性,然后返回该属性值.请查看我的代码:
I'm attempting to use BeautifulSoup so get a list of HTML <div>
tags, then check if they have a name attribute and then return that attribute value. Please see my code:
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
我的问题是如何获取name属性的值?
My question is how do I get the value of the name attribute?
推荐答案
使用以下代码,它应该可以工作
Use the following code, it should work
nameTags = soup.findAll('div',{"name":True})
for n in nameTags:
# Do your processing
这篇关于获取标签列表并在BeautifulSoup中获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文