首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python的“美丽汤”中使用自定义解析器?

如何在Python的“美丽汤”中使用自定义解析器?
EN

Stack Overflow用户
提问于 2017-12-18 07:24:43
回答 1查看 1.1K关注 0票数 4

我正在使用美汤4解析和修改一组HTML文件。HTML文件是角模板,这意味着它们的标记在某种程度上不同于常规HTML文档中的标记(混合大小写属性、指令、输入/输出绑定等)。

美丽汤文档 (html.parser,lxml,html5lib)中列出的解析器没有一个完全符合我的需求。最接近它的是在html.parser中构建的Python,但是我不得不对它做一些调整。

是否可以使用自定义解析器类与美丽汤?如果是,如何实施?

编辑:下面的是美丽汤如何解析模板的一个例子。主要问题是结果的所有属性都是小写(*ngIf -> *ngif)和指令(appAutoFocus、appKeepFocusInside等)。获取一个空字符串值(例如appautofocus="")。

代码语言:javascript
复制
from bs4 import BeautifulSoup

test_html = """
<kendo-dialog *ngIf="dialogOpened" appAutoFocus appKeepFocusInside (close)="closeDialog()" width="1000">
   <kendo-dialog-titlebar>A title</kendo-dialog-titlebar>
   <div *ngIf="model.showValidationFailedMessage" class="alert alert-danger alert-dismissible">
      <button type="button" class="close" aria-label="Close" (click)="model.closeValidationAlert()"><span
         aria-hidden="true">&amp;times;</span></button>
      Validation failed
   </div>

   <kendo-tabstrip [keepTabContent]="true">
      <kendo-tabstrip-tab title="Tab title 1" [selected]="true">
         <ng-template kendoTabContent>
            <div>Tab content</div>
         </ng-template>
      </kendo-tabstrip-tab>
   </kendo-tabstrip>
</kendo-dialog>
"""

document = BeautifulSoup(test_html, "html.parser")
print(document.prettify())

结果:

代码语言:javascript
复制
<kendo-dialog (close)="closeDialog()" *ngif="dialogOpened" appautofocus="" appkeepfocusinside="" width="1000">
 <kendo-dialog-titlebar>
  A title
 </kendo-dialog-titlebar>
 <div *ngif="model.showValidationFailedMessage" class="alert alert-danger alert-dismissible">
  <button (click)="model.closeValidationAlert()" aria-label="Close" class="close" type="button">
   <span aria-hidden="true">
    &amp;times;
   </span>
  </button>
  Validation failed
 </div>
 <kendo-tabstrip [keeptabcontent]="true">
  <kendo-tabstrip-tab [selected]="true" title="Tab title 1">
   <ng-template kendotabcontent="">
    <div>
     Tab content
    </div>
   </ng-template>
  </kendo-tabstrip-tab>
 </kendo-tabstrip>
</kendo-dialog>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-20 09:34:31

您可以使用bs4.builder.register_treebuilders_from。内置构建器是bs4.builder包中的模块。例如,bs4.builder._lxml

这是register_treebuilders_from

代码语言:javascript
复制
def register_treebuilders_from(module):
    """Copy TreeBuilders from the given module into this module."""
    # I'm fairly sure this is not the best way to do this.
    this_module = sys.modules['bs4.builder']
    for name in module.__all__:
        obj = getattr(module, name)

        if issubclass(obj, TreeBuilder):
            setattr(this_module, name, obj)
            this_module.__all__.append(name)
            # Register the builder while we're at it.
            this_module.builder_registry.register(obj)

以及软件包根模块的尾部。

代码语言:javascript
复制
# Builders are registered in reverse order of priority, so that custom
# builder registrations will take precedence. In general, we want lxml
# to take precedence over html5lib, because it's faster. And we only
# want to use HTMLParser as a last result.
from . import _htmlparser
register_treebuilders_from(_htmlparser)
try:
    from . import _html5lib
    register_treebuilders_from(_html5lib)
except ImportError:
    # They don't have html5lib installed.
    pass
try:
    from . import _lxml
    register_treebuilders_from(_lxml)
except ImportError:
    # They don't have lxml installed.
    pass

因此,您需要创建一个带有bs4.builder.TreeBuilder子类的模块,并将其注册到模块的__all__中。然后将模块传递给register_treebuilders_from

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47863781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档