我尝试使用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
我的问题是如何获取name属性的值?
发布于 2012-05-29 19:03:58
使用下面的代码,它应该可以工作
nameTags = soup.findAll('div',{"name":True})
for n in nameTags:
# Do your processing
发布于 2012-05-29 19:10:42
谢谢你们都想明白了
n['name']
发布于 2014-07-01 20:53:36
为了便于将来参考,以下是要用作单个答案的代码:
soup = BeautifulSoup(html)
nameTags = soup.findAll('div',{"name":True})
for n in nameTags:
name = n['name']
# Do your processing
传递{"name":True}
的第二个参数会将结果限制为具有name
属性的div
标记。如果要查找具有name
标记的特定值的标记,则可以传递{"name":"specificNameValue"}
https://stackoverflow.com/questions/10797741
复制相似问题