如何找到只有某些属性的标签 - BeautifulSoup [英] How to find tags with only certain attributes - BeautifulSoup

查看:30
本文介绍了如何找到只有某些属性的标签 - BeautifulSoup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 BeautifulSoup 搜索仅包含我搜索的属性的标签?

How would I, using BeautifulSoup, search for tags containing ONLY the attributes I search for?

例如,我想查找所有 标签.

For example, I want to find all <td valign="top"> tags.

以下代码:raw_card_data = soup.fetch('td', {'valign':re.compile('top')})

获取我想要的所有数据,但也获取任何具有属性 valign:top

gets all of the data I want, but also grabs any <td> tag that has the attribute valign:top

我也试过:raw_card_data = soup.findAll(re.compile('<td valign="top">'))这没有返回任何内容(可能是因为正则表达式不好)

I also tried: raw_card_data = soup.findAll(re.compile('<td valign="top">')) and this returns nothing (probably because of bad regex)

我想知道 BeautifulSoup 中是否有一种方法可以说查找 标签,其唯一属性是 valign:top"

I was wondering if there was a way in BeautifulSoup to say "Find <td> tags whose only attribute is valign:top"

更新例如,如果一个 HTML 文档包含以下 标签:

UPDATE FOr example, if an HTML document contained the following <td> tags:

<td valign="top">.....</td><br />
<td width="580" valign="top">.......</td><br />
<td>.....</td><br />

我只想返回第一个 <td> 标签(<td width="580" valign="top">)

I would want only the first <td> tag (<td width="580" valign="top">) to return

推荐答案

BeautifulSoup 文档

你可以使用这个:

soup = BeautifulSoup(html)
results = soup.findAll("td", {"valign" : "top"})

返回只有 valign="top" 的标签属性,你可以检查标签的长度 attrs 属性:

To return tags that have only the valign="top" attribute, you can check for the length of the tag 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>

这篇关于如何找到只有某些属性的标签 - BeautifulSoup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆