python - 美汤问题

标签 python html-parsing beautifulsoup

我想获取 HTML 文档中的特定行

行设置了以下属性:bgcolor 和 vallign

这是 HTML 表格的片段:

<table>
   <tbody>
      <tr bgcolor="#f01234" valign="top">
        <!--- td's follow ... -->
      </tr>
      <tr bgcolor="#c01234" valign="top">
        <!--- td's follow ... -->
      </tr>
   </tbody>
</table>

我快速浏览了BS's documentation .不清楚要传递什么参数给 findAll 以匹配我想要的行。

有谁知道 findAll() 需要什么 tp bass 来匹配我想要的行?

最佳答案

不要使用正则表达式来解析 html。使用 html 解析器

import lxml.html
doc = lxml.html.fromstring(your_html)
result = doc.xpath("//tr[(@bgcolor='#f01234' or @bgcolor='#c01234') "
    "and @valign='top']")
print result

这将从您的 html 中提取所有匹配的 tr 元素,您可以对它们进行进一步的操作,例如更改文本、属性值、提取、进一步搜索...

必填链接:

RegEx match open tags except XHTML self-contained tags

关于python - 美汤问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4658686/

相关文章:

python - 如何使用 Beautiful Soup 查找和更改标签之外的文本?

python - 是否可以在 python 中重载多重比较语法?

python - 如何告诉 python HTMLParser 停止

python - 缓慢的 html 解析器。如何提高速度?

python - HTML 表格与 python 美丽汤

Python Mechanize 无法识别表单

python - 使用脚本在 IDLE 中设置变量

python - Python 中 subprocess.PIPE 的非阻塞读取

python - 如何使用 Zeep 和 Python 3.7 捕获错误

python - 如何在 python 中扩展字符串中的字符串?