python - 2件套搭配美汤精选

标签 python html beautifulsoup

我的表格中有以下 html,其中明暗行交替:

<tr class="light">
    <td>....
    </td>
  </tr>

  <tr class="dark">
    <td>....

我可以使用以下方法获得所有亮行或所有暗行:

soup.select('tr.light') or soup.select('tr.dark')

有没有办法将两者结合在一个语句中?

最佳答案

美汤支持CSS selectors ,所以它应该只允许您使用 grouping .

如果是这样,那么您可以通过以下方式选择两者:

soup.select('tr.dark, tr.light')  # note the comma between.

编辑: Beautiful soup 似乎对 CSS 选择器的支持相当有限,所以你也可以尝试这个:

import re
soup.find_all("td" class_=re.compile("light|dark"))

另一种选择是 lxml应该支持全系列的 CSS 选择器。

关于python - 2件套搭配美汤精选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25438771/

相关文章:

javascript - 选择选择选项时如何显示隐藏的 div?

javascript - 选择选择选项后移动自动对焦

python - 使用 BeautifulSoup 获取元素内的实际内容

python - 为什么这会返回 False

python - 在 Google Docs API Python 中插入表格

html - 如何将图像居中放置在容器的正中心(Bootstrap)?

python - 让 BeautifulSoup 跳过我的 html 的一部分..或拆分它

python - BeautifulSoup 导航忽略指定路径

python - 定期查找具有最高列值的 n 行

python - wxpython 线程框架初始化错误