我正在为工作而抓取一个网站,我不能得到美丽的汤来抓取不寻常的标签之间的某些文本。
我简单地搜索了一个span标记,它显示在结果中,但是我不能很快使用re.compile获得要显示的特定单词。
这是html的一个片段。
ng-hide="col.isHidden || col.alwaysHide" ng-class="{'td-content-title':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="{'td-content-title':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="{'td-content-title':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="{'td-content-title':col.isContentTitle}" 这是用来抓取所有span标签的代码
soup.find_all('span')但是当我使用像这样的东西时
soup.find_all('span', {re.compile('Passed|Failed')}):它似乎没有给出结果
我也试过了
soup.find_all('span', {'test-case-result status-2': re.compile('Passed|Failed')})Expected将抓取所有通过和失败的实例
Actual -除纯粹使用span are之外的所有抓取尝试都显示为空。
我确信这很简单,我遗漏了一些东西,但我真的在努力进一步了解文档。谢谢你的帮助。
发布于 2019-04-07 06:00:33
在find_all()中使用text=
soup.find_all('span', text=re.compile('Passed|Failed'))如果没有text=,它可能会使用regex来搜索标记名。
发布于 2019-04-07 13:00:12
使用bs 4.7.1时,我会避免使用正则表达式,而使用:contains伪类
from bs4 import BeautifulSoup
html = '''
ng-hide="col.isHidden || col.alwaysHide" ng-class="{'td-content-title':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="{'td-content-title':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="{'td-content-title':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="{'td-content-title':col.isContentTitle}"
'''
soup = BeautifulSoup(html, 'lxml')
spans = soup.select('span:contains(Passed),span:contains(Failed)')
print(spans)https://stackoverflow.com/questions/55553546
复制相似问题