簡體   English   中英

將元素字符串中的“ \\ n”替換為 <br> 在Beautifulsoup中標記

[英]Replace “\n” in element string with <br> tag in Beautifulsoup

我正在創建一個新標簽,並用換行符分配一個字符串

from bs4 import BeautifulSoup

soup = BeautifulSoup("", "html.parser")

myTag = soup.new_tag("div")
myTag.string = "My text \n with a new line"

soup.insert(0, myTag)

結果是

<div>My text 
 with a new line</div>

如預期的那樣。 但是,換行符需要<br>標記才能正確呈現。

我該如何實現?

我認為最好將CSS 空白屬性設置為在該div上進行pre-wrap

pre- wrap-空白由瀏覽器保留。 文本將在必要時自動換行,並在換行符上顯示。

一個例子:

<div style="white-space:pre-wrap"> Some \n text here </div>

以及在BeautifulSoup中執行此操作的代碼:

myTag = soup.new_tag("div", style="white-space:pre-wrap")
myTag.string = "My text \n with a new line"

似乎替換\\n並非易事,因為BeautifulSoup默認情況下會轉義HTML實體。 一種替代方法是分割輸入字符串,並使用文本和<br>標記自行構建標記結構:

def replace_newline_with_br(s, soup):
    lines = s.split('\n')
    div = soup.new_tag('div')
    div.append(lines[0])
    for l in lines[1:]:
        div.append(soup.new_tag('br'))
        div.append(l)
    soup.append(div)

mytext = "My text with a few \n newlines \n"
mytext2 = "Some other text \n with a few more \n newlines \n here"

soup = BeautifulSoup("", )
replace_newline_with_br(mytext, soup)
replace_newline_with_br(mytext2, soup)
print soup.prettify()     

印刷品:

<div>
 My text with a few
 <br/>
 newlines
 <br/>
</div>
<div>
 Some other text
 <br/>
 with a few more
 <br/>
 newlines
 <br/>
 here
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM