Python3 Beautiful Soup get HTML tag anchor
<p>I am trying to use BS4 and Python to save and replace the content of the first <code>&lt;translate&gt;</code> tag in a HTML file.</p>

<p>Now I am trying to do something like this:</p>

<pre><code>translate_bs4 = bs4_object.find('translate')
translate_key = '{{ key }}'
translate_initial = str(title_bs4)
translate_bs4.string = translate_key
</code></pre>

<p>My test case is:</p>

<pre><code>&lt;translate&gt;tag with &lt;other_tag&gt;some text&lt;/other_tag&gt;&lt;/translate&gt;
&lt;much_longer_file&gt;...&lt;/much_longer_file&gt;
</code></pre>

<p>and the HTML is the expected one of:</p>

<pre><code>&lt;translate&gt;{{ key }}&lt;/translate&gt;
&lt;much_longer_file&gt;...&lt;/much_longer_file&gt;
</code></pre>

<p>but the value of <code>translate_initial</code> is </p>

<pre><code>&lt;translate&gt;tag with &lt;other_tag&gt;some text&lt;/other_tag&gt;&lt;/translate&gt;
</code></pre>

<p>instead of expected</p>

<pre><code>tag with &lt;other_tag&gt;some text&lt;/other_tag&gt;
</code></pre>

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

<p>Try this:</p>

<pre><code>translate_bs4 = bs4_object.find('translate')
translate_initial = translate_bs4.decode_contents(formatter="html")
</code></pre>

python html beautifulsoup