私はBeautifulSoup 4を使用して、HTMLファイルのセットを解析および変更しています。HTMLファイルはAngularテンプレートです。つまり、マークアップは通常のHTMLドキュメントのマークアップ(大文字と小文字が混在する属性、ディレクティブ、入出力バインディングなど)とは多少異なります。
Beautiful Soupsのドキュメント(html.parser、lxml、html5lib)にリストされているパーサーはどれも私のニーズに完全には適合しません。最も近いのはPythonの組み込みhtml.parserですが、いくつか調整する必要がありました。
Beautiful Soupでカスタムパーサークラスを使用することは可能ですか?はいの場合、これはどのように実装できますか?
編集:以下は、BeautifulSoupがテンプレートを解析する方法の例です。主な問題は、結果のすべての属性が小文字(* ngIf-> * ngif)であり、ディレクティブ(appAutoFocus、appKeepFocusInsideなど)が空の文字列値(appautofocus = ""など)を取得することです。
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">&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())
結果:
<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">
&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>
を使用できますbs4.builder.register_treebuilders_from
。組み込みビルダーは、bs4.builder
パッケージ内のモジュールです。たとえば、bs4.builder._lxml
。
これがregister_treebuilders_from
。
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
登録する必要__all__
があります。次に、モジュールをに渡しますregister_treebuilders_from
。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加