我正在尝试使用BS4和Python保存和替换<translate>
HTML文件中第一个标签的内容。
现在,我正在尝试执行以下操作:
translate_bs4 = bs4_object.find('translate')
translate_key = '{{ key }}'
translate_initial = str(title_bs4)
translate_bs4.string = translate_key
我的测试用例是:
<translate>tag with <other_tag>some text</other_tag></translate>
<much_longer_file>...</much_longer_file>
HTML是下列之一:
<translate>{{ key }}</translate>
<much_longer_file>...</much_longer_file>
但的价值translate_initial
是
<translate>tag with <other_tag>some text</other_tag></translate>
而不是预期的
tag with <other_tag>some text</other_tag>
我知道可以使用正则表达式轻松提取它,但是我想要更多与DOM相关的解决方案。
试试这个:
translate_bs4 = bs4_object.find('translate')
translate_initial = translate_bs4.decode_contents(formatter="html")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句