Regex 如何使用BS4捕获异常跨度标记中的数据?

Regex 如何使用BS4捕获异常跨度标记中的数据?,regex,python-3.x,web-scraping,beautifulsoup,Regex,Python 3.x,Web Scraping,Beautifulsoup,我正在为工作抓取一个网站,但我无法用BeautifulSoup在不寻常的标签之间抓取某些文本 我只搜索了一个span标记,它显示在结果中,但是我无法在不久之后使用re.compile获得要显示的特定单词 这是html的一个片段 ng-hide="col.isHidden || col.alwaysHide" ng-class="{'td-content-title':col.isContentTitle}" responsive-table-cell="ctrl.

我正在为工作抓取一个网站,但我无法用BeautifulSoup在不寻常的标签之间抓取某些文本

我只搜索了一个span标记,它显示在结果中,但是我无法在不久之后使用re.compile获得要显示的特定单词

这是html的一个片段

ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Result " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"><span class="test-case-result status-2">Passed</span></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Approval " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"><span class="test-case-approval-status status-1">Pending</span></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Time Left " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" 
但是当我使用

soup.find_all('span', {re.compile('Passed|Failed')}):
它似乎没有结果

我也试过了

soup.find_all('span', {'test-case-result status-2': re.compile('Passed|Failed')})
预期-所有通过和失败的实例都将被删除

实际-除纯粹使用span tage外,所有刮取尝试都显示为空

我确信这很简单,我遗漏了一些东西,但我真的很难在文档中获得进一步的信息。谢谢您的帮助。

在“查找全部”中使用text=


如果没有text=它可以使用正则表达式来搜索标记名。

对于bs 4.7.1,我将避免使用正则表达式并使用:contains伪类

from bs4 import BeautifulSoup
html = '''
  ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Result " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"><span class="test-case-result status-2">Passed</span></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Approval " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"><span class="test-case-approval-status status-1">Pending</span></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Time Left " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}"
  '''
soup = BeautifulSoup(html, 'lxml')

spans =  soup.select('span:contains(Passed),span:contains(Failed)')
print(spans)

将url添加到此页面。soup.find_all'span',text=re.compile'Passed | Failed'@furas我不能,恐怕这是为了工作,所以它在登录后walltry text=re.compile。。。发现_all@furas你明白了。非常感谢。你能把它作为一个答案打出来让我接受吗?谢谢@QHarr。你为什么不使用正则表达式?是否包含只是更容易使用?我相信在这种情况下,它应该比正则表达式更快
soup.find_all('span', text=re.compile('Passed|Failed'))
from bs4 import BeautifulSoup
html = '''
  ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Result " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"><span class="test-case-result status-2">Passed</span></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Approval " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}" responsive-table-cell="ctrl.getCellData(col, row)" aria-hidden="false"><span class="test-case-approval-status status-1">Pending</span></td><!----><td ng-repeat="col in ctrl.tableConfig.columns" data-column-title="Time Left " ng-hide="col.isHidden || col.alwaysHide" ng-class="{&#39;td-content-title&#39;:col.isContentTitle}"
  '''
soup = BeautifulSoup(html, 'lxml')

spans =  soup.select('span:contains(Passed),span:contains(Failed)')
print(spans)