如何使用Beautifulsoup在一些文本中嵌入p标签?

时间:2009-02-09 10:09:06

标签: python beautifulsoup

我想嵌入<p&gt;标记哪里有\ r \ n \ r \ n。

u“最后斯里兰卡出现了,阻止了他们的第一场5-0系列粉饰,并且连续9场ODI胜利阻止了印度队。\ r \ n \ r \ n对于62球,Yuvraj Singh发挥了梦想,敲打印度尽管哨子在他周围摔倒,但在比赛中仍然很糟糕。这也是Mahela Jayawardene在过去的11场比赛中第二次用硬币击败Mahendra Singh Dhoni。\ r \ n \ n \ n \ n \ n \ n \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r \ n是Jayasuriya为斯里兰卡提供了跳板。\ r \ n \ r \ n烟火可能已经停止了Jayasuriya的解雇,但运行仍然以合理的速度进行。“

我尝试使用BeautifulSoup解决这个问题,但无法找到解决方法。任何人都可以通过这一点了解。提前谢谢。

1 个答案:

答案 0 :(得分:5)

''.join('<p>%s</p>' % line for line in text.split('\r\n\r\n'))
# Results:
u"<p>Finally Sri Lanka showed up, prevented their first 5-0
series whitewash, and stopped India at nine ODI wins in a row. </p>
<p>For 62 balls Yuvraj Singh played a dream knock, keeping India in the 
game despite wickets falling around him. </p><p>Perhaps the toss played
a big part. This was only the second time Mahela Jayawardene beat Mahendra
Singh Dhoni with the coin in the last 11 occasions. </p>
<p>It was Jayasuriya who provided Sri Lanka with the springboard. </p>
<p>The pyrotechnics may have stopped upon Jayasuriya's dismissal, but 
the runs kept coming at a fair pace.</p>"