如何将自定义解析器与Python';汤很好吗?

如何将自定义解析器与Python';汤很好吗?,python,python-3.x,parsing,beautifulsoup,html-parsing,Python,Python 3.x,Parsing,Beautifulsoup,Html Parsing,我正在使用解析和修改一组HTML文件。HTML文件是角度模板,这意味着它们的标记在某种程度上不同于常规HTML文档中的标记(混合大小写属性、指令、输入/输出绑定等) 在BeautifulSoups(html.parser、lxml、html5lib)中列出的解析器没有一个完全符合我的需要。最接近它的是Python内置的html.parser,但我不得不对它做一些调整 是否可以将自定义解析器类与Beautiful Soup一起使用?如果是,如何实施 编辑:下面是Beauty Soup解析模板的示例

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

在BeautifulSoups(html.parser、lxml、html5lib)中列出的解析器没有一个完全符合我的需要。最接近它的是Python内置的html.parser,但我不得不对它做一些调整

是否可以将自定义解析器类与Beautiful Soup一起使用?如果是,如何实施

编辑:下面是Beauty Soup解析模板的示例。主要问题是结果中的所有属性都是小写的(*ngIf->*ngIf),并且指令(appAutoFocus、appKeepFocusInside等)得到一个空字符串值(例如appAutoFocus=“”)

从bs4导入美化组
test_html=“”
头衔
&安倍;
验证失败
选项卡内容
"""
document=BeautifulSoup(test_html,“html.parser”)
打印(document.prettify())
结果:

<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>

头衔
&;时代;
验证失败
选项卡内容
您可以使用。内置构建器是包中的模块。例如

这里是从注册树生成器

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)
和包的根模块的尾部

# 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
的模块,并将其注册到模块的
\uuuuuuuuuuu
中。然后将模块从

传递到
注册树生成器\u。您是否尝试使用带角度的BS?你有问题吗?在示例中显示。@furas我在帖子中添加了一个示例。
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)