Filip Ion Dorinel Filip Ion Dorinel - 3 months ago 7
HTML Question

Python3 Beautiful Soup get HTML tag anchor

I am trying to use BS4 and Python to save and replace the content of the first

<translate>
tag in a HTML file.

Now I am trying to do something like this:

translate_bs4 = bs4_object.find('translate')
translate_key = '{{ key }}'
translate_initial = str(title_bs4)
translate_bs4.replaceWith(translate_key)


My test case is:

<translate>tag with <other_tag>some text</other_tag></translate>
<much_longer_file>...</much_longer_file>


and the HTML is the expected one of:

<translate>{{ key }}</translate>
<much_longer_file>...</much_longer_file>


but the value of
translate_initial
is

<translate>tag with <other_tag>some text</other_tag></translate>


instead of expected

tag with <other_tag>some text</other_tag>


I know that it can be easy extracted with a regex, but I want a some more DOM-related solution.

Answer

Try this:

translate_bs4 = bs4_object.find('translate')
translate_initial = translate_bs4.decode_contents(formatter="html")