靓汤问题

2024-10-03 13:20:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

vallign行和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什么参数来匹配我想要的行。在

有人知道什么样的tp bass to findAll()来匹配我想要的行吗?在


Tags: 文档属性tophtmltabletrtdfollow
2条回答

有点像

soup.findAll('tr', attrs={'bgcolor': re.compile(r'#f01234|#c01234'), 'valign': 'top'})

不要使用regex解析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

相关问题 更多 >