python - 确定 TD 标签内的类

标签 python python-3.x beautifulsoup

使用 python beautifulsoup 我试图找到所有 <tr> HTML 页面的标签。但是我想过滤掉任何 <tr><td> 之一中具有特定类的标签标签。

我试图过滤出 <td> 中具有“警告”类的行使用以下代码标记。

soup = BeautifulSoup(data, 'html.parser')
print(soup.find_all('tr', class_=lambda c: 'Warning' not in c))

我知道它没有过滤掉“警告类”,因为我正在使用 <tr>find_all里面功能,但如果我尝试使用 td它给了我一个TypeError: argument of type 'NoneType' is not iterable .

如有任何想法,我们将不胜感激。

from bs4 import BeautifulSoup

data = '''
<tr role="row" class="odd red" data-id="32">
   <td role="gridcell" class="Warning">33</td>
   <td role="gridcell">Ralph</td>
   <td role="gridcell">List 2</td>
   <td role="gridcell">FE</td>
   <td role="gridcell">07/12/1996</td>
</tr>
<tr role="row" class="even red" data-id="33">
   <td role="gridcell">34</td>
   <td role="gridcell">Mary</td>
   <td role="gridcell">List 2</td>
   <td role="gridcell">SOTLTM</td>
   <td role="gridcell">08/12/1996</td>
</tr>
<tr role="row" class="odd red" data-id="34">
   <td role="gridcell">35</td>
   <td role="gridcell">Tom</td>
   <td role="gridcell">List 2</td>
   <td role="gridcell">SOTLTM</td>
   <td role="gridcell">09/12/1996</td>
</tr>
'''

soup = BeautifulSoup(data, 'html.parser')
print(soup.find_all('td', class_=lambda c: 'Warning' not in c))

最佳答案

class=不是大多数 <td> 的属性元素。这导致 c设置为 None在你的 lambda 中,这样你就可以通过条件测试自动让它们通过过滤器:

print(soup.find_all('td', class_=lambda c: not c or 'Warning' not in c))
#                                          ^^^^^^^^

输出

[<td role="gridcell">Ralph</td>, 
 <td role="gridcell">List 2</td>, 
 <td role="gridcell">FE</td>, 
 <td role="gridcell">07/12/1996</td>, 
 <td role="gridcell">34</td>, 
 <td role="gridcell">Mary</td>, 
 <td role="gridcell">List 2</td>, 
 <td role="gridcell">SOTLTM</td>, 
 <td role="gridcell">08/12/1996</td>, 
 <td role="gridcell">35</td>, 
 <td role="gridcell">Tom</td>, 
 <td role="gridcell">List 2</td>, 
 <td role="gridcell">SOTLTM</td>, 
 <td role="gridcell">09/12/1996</td>]

从那里开始,我们可以根据您的主要关注点应用此条件,即过滤 <tr>根据他们的 child 的元素:

soup = BeautifulSoup(data, 'html.parser')

for tr in soup.find_all('tr'):
    if not bool(tr.find_all('td', class_=lambda c: c and 'Warning' in c)):
        print(tr) # or print(tr.find_all('td')) if you'd like to 
                  # access only the children of the filtered <tr>s

输出

<tr class="even red" data-id="33" role="row">
<td role="gridcell">34</td>
<td role="gridcell">Mary</td>
<td role="gridcell">List 2</td>
<td role="gridcell">SOTLTM</td>
<td role="gridcell">08/12/1996</td>
</tr>
<tr class="odd red" data-id="34" role="row">
<td role="gridcell">35</td>
<td role="gridcell">Tom</td>
<td role="gridcell">List 2</td>
<td role="gridcell">SOTLTM</td>
<td role="gridcell">09/12/1996</td>
</tr>

关于python - 确定 TD 标签内的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636434/

相关文章:

python - 使用出生日期从 Django 模型中过滤人员

python - 将嵌套 json 读取到数据框中

来自文本文件的 Python 字典

python - 为什么需要在循环内创建可变随机函数才能正常工作?

python - 如何抓取两个 URL 并将每个 URL 的元素放入一个表中?

python - 为什么 PyImport_Import 无法从当前目录加载模块?

python - 在python中将大图像文件读取为数组

Python - 读取msgpack文件并将其存储到数据框中

python - 在 python 中先抓取后如何移动到第二页

python - 通过导入时间加快漂亮汤的速度(抓取太多不相关的数据)