问题描述
如何使用 BeautifulSoup 搜索仅包含我搜索的属性的标签?
How would I, using BeautifulSoup, search for tags containing ONLY the attributes I search for?
例如,我想查找所有 For example, I want to find all 以下代码: 获取我想要的所有数据,但也获取任何具有属性 gets all of the data I want, but also grabs any 我也试过: I also tried: 我想知道 BeautifulSoup 中是否有一种方法可以说查找 I was wondering if there was a way in BeautifulSoup to say "Find 更新例如,如果一个 HTML 文档包含以下 UPDATEFOr example, if an HTML document contained the following 我只想返回第一个 I would want only the first 你可以使用这个: 返回只有 valign="top" 的标签属性,你可以检查标签的长度 To return tags that have only the valign="top" attribute, you can check for the length of the tag 返回: 这篇关于如何找到只有某些属性的标签 - BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 标签. <td valign="top">
tags. raw_card_data = soup.fetch('td', {'valign':re.compile('top')})
valign:top
<td>
tag that has the attribute valign:top
raw_card_data = soup.findAll(re.compile('<td valign="top">'))
这没有返回任何内容(可能是因为正则表达式不好)raw_card_data = soup.findAll(re.compile('<td valign="top">'))
and this returns nothing (probably because of bad regex) 标签,其唯一属性是 valign:top
"<td>
tags whose only attribute is valign:top
" 标签: <td>
tags:<td valign="top">.....</td><br />
<td width="580" valign="top">.......</td><br />
<td>.....</td><br />
<td>
标签(<td width="580" valign="top">
)<td>
tag (<td width="580" valign="top">
) to return推荐答案
soup = BeautifulSoup(html)
results = soup.findAll("td", {"valign" : "top"})
attrs
属性:attrs
property :from BeautifulSoup import BeautifulSoup
html = '<td valign="top">.....</td>
<td width="580" valign="top">.......</td>
<td>.....</td>'
soup = BeautifulSoup(html)
results = soup.findAll("td", {"valign" : "top"})
for result in results :
if len(result.attrs) == 1 :
print result
<td valign="top">.....</td>