【问题标题】:How to capture data in unusual span tag with BS4?如何使用 BS4 捕获异常跨度标签中的数据?
【发布时间】:2019-04-06 21:16:15
【问题描述】:

我正在为工作抓取一个网站,但我无法让 Beautiful soup 抓取异常标签之间的某些文本。

我只是搜索了一个 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')

但是当我使用类似的东西时

soup.find_all('span', {re.compile('Passed|Failed')}):

似乎没有结果

我也试过了

soup.find_all('span', {'test-case-result status-2': re.compile('Passed|Failed')})

预期 - 所有通过和失败的实例都将被删除

实际 - 除了纯粹使用 span tage 之外的所有抓取尝试都显示为空。

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

【问题讨论】:

  • 将网址添加到此页面。
  • soup.find_all('span', text=re.compile('Passed|Failed'))
  • @furas 我不能担心它的工作,所以它在登录墙后面
  • find_all()中尝试text=re.compile(...)
  • @furas 你明白了。非常感谢。你能输入它作为答案,以便我接受吗?

标签: regex python-3.x web-scraping beautifulsoup


【解决方案1】:

find_all() 中使用text=

soup.find_all('span', text=re.compile('Passed|Failed'))

如果没有text=,它可以使用regex 来搜索标签名称。

【讨论】:

    【解决方案2】:

    使用 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)
    

    【讨论】:

    • 谢谢@QHarr。你会避免使用正则表达式的原因是什么?包含更容易使用吗?
    • 我相信在这种情况下它应该比正则表达式更快
    猜你喜欢
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多