chon
chon

Reputation: 13

escape problem with beautifulsoup in python

I am using the beautifulsoup4 in python in order to update the table on the confluence page when I use the soup.append(str) function, the < > were escaped and became &lt; &gt; so that i can not update the table correctly. Could someone give me tips? Or maybe some better solution for update the table on the confluence page, thanks in advance. :)

what i expect:

<tr>"string"</tr>

what the result:

&lt;tr &gt;"string"&lt; tr&gt;

Upvotes: 1

Views: 732

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195408

That happens because you append string to other tag. The solution is to create new_tag() or whole new soup.

For example:

txt = '''
<div>To this tag I append other tag</div>
'''

soup = BeautifulSoup(txt, 'html.parser')
soup.find('div').append( BeautifulSoup('<tr>string</tr>', 'html.parser') )

print(soup.prettify())

Prints:

<div>
 To this tag I append other tag
 <tr>
  string
 </tr>
</div>

Upvotes: 1

Related Questions