如何使用beautifulsoup将所有<img/>标签与<p>标签括起来?

时间:2016-04-14 08:36:35

标签: python beautifulsoup

我的html内容中包含一些<img>个标签。我要将<img>标记中的所有<p>标记括起来。有没有做过这样的事情。

1 个答案:

答案 0 :(得分:-1)

str1='<img>abc</img><img>def</img>'

str1=str1.replace('<img>','<p><img>')
str1=str1.replace('</img>','</img></p>')
print str1
#<p><img>abc</img></p><p><img>def</img></p>