【问题标题】:How to embed p tag inside some text using Beautifulsoup?如何使用 Beautifulsoup 在一些文本中嵌入 p 标签?
【发布时间】:2010-10-06 08:55:16
【问题描述】:

我想在有 \r\n\r\n 的地方嵌入 <p> 标签。

u"终于斯里兰卡出现了,阻止了他们的第一个 5-0 系列粉饰,并在 ODI 连续九场胜利中阻止了印度。\r\n\r\nYuvraj Singh 打了 62 球,打出了梦幻般的击球,让印度保持尽管三柱门落在他周围,但他仍然在比赛中。\r\n\r\n也许抛掷起了很大的作用。这只是 Mahela Jayawardene 在过去 11 场比赛中第二次用硬币击败 Mahendra Singh Dhoni。\r\n\ r\n是 Jayasuriya 为斯里兰卡提供了跳板。\r\n\r\n在 Jayasuriya 被解雇后,烟火可能已经停止,但奔跑的速度一直保持平稳。”

我尝试使用 BeautifulSoup 解决此问题,但找不到解决方法。任何人都可以对此有所了解。提前致谢。

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:
    ''.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>"
    

    【讨论】:

    • 你可能会在拆分分隔符上添加空间,只是为了让它更干净
    • @SilentGhost:这适用于示例文本,但不是所要求的。也许使用 '

      %s

      ' % line.strip()...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2014-10-02
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多