我正在使用漂亮的湯和請求來加載網站的 HTML(例如https://en.wikipedia.org/wiki/Elephant)。我想模仿這個頁面,但我想為“p”標簽(段落)中的句子著色。
為此,我使用 spacy 將文本分解為句子。我選擇一種顏色(一種基于二進制深度學習分類器的概率顏色,供感興趣的人使用)。
def get_colorized_p(p):
doc = nlp(p.text) # p is the beautiful soup p tag
string = '<p>'
for sentence in doc.sents:
# The prediction value in anything within 0 to 1.
prediction = classify(sentence.text, model=model, pred_values=True)[1][1].numpy()
# I am using a custom function to map the prediction to a hex colour.
color = get_hexcolor(prediction)
string = f'<mark style="background: {color};">{sentence.text} </mark> '
string = '</p>'
return string # I create a new long string with the markup
我在 p 標簽中創建了一個帶有 HTML 標記的新長字串。我現在想替換漂亮湯物件中的“舊”元素。我用一個簡單的回圈來做到這一點:
for element in tqdm_notebook(soup.findAll()):
if element.name == 'p':
if len(element.text.split()) > 2:
element = get_colorized_p(element)
但是,當我呈現 HTML 檔案時,這不會產生任何錯誤。HTML 檔案不帶標記顯示
我正在使用 jupyter 快速顯示 HTML 檔案:
from IPython.display import display, HTML
display(HTML(html_file))
但是,這不起作用。我確實驗證了回傳的字串get_colorized_p。當我在單個 p 元素上使用它并渲染它時,它作業正常。但我想將字串插入到美麗的湯物件中。
我希望任何人都可以對這個問題有所了解。替換回圈內的元素會出錯。但是,我不知道如何解決它。
以防萬一的渲染字串示例:
<p><mark style="background: #edf8fb;">Elephants are the largest existing land animals.</mark><mark style="background: #f1fafc;">Three living species are currently recognised: the African bush elephant, the African forest elephant, and the Asian elephant.</mark><mark style="background: #f3fafc;">They are an informal grouping within the proboscidean family Elephantidae.</mark><mark style="background: #f3fafc;">Elephantidae is the only surviving family of proboscideans; extinct members include the mastodons.</mark><mark style="background: #eff9fb;">Elephantidae also contains several extinct groups, including the mammoths and straight-tusked elephants.</mark><mark style="background: #68c3a6;">African elephants have larger ears and concave backs, whereas Asian elephants have smaller ears, and convex or level backs.</mark><mark style="background: #56ba91;">The distinctive features of all elephants include a long proboscis called a trunk, tusks, large ear flaps, massive legs, and tough but sensitive skin.</mark><mark style="background: #d4efec;">The trunk is used for breathing, bringing food and water to the mouth, and grasping objects.</mark><mark style="background: #e7f6f9;">Tusks, which are derived from the incisor teeth, serve both as weapons and as tools for moving objects and digging.</mark><mark style="background: #d9f1f0;">The large ear flaps assist in maintaining a constant body temperature as well as in communication.</mark><mark style="background: #e5f5f9;">The pillar-like legs carry their great weight.</mark><mark style="background: #72c7ad;"> </mark></p>
uj5u.com熱心網友回復:
喜歡這個想法和配色方案 - 我認為主要問題是你嘗試tag用 a替換string,而你應該replace_with()abs4 object為你soup獲得新的味道增添趣味:
for element in tqdm_notebook(soup.find_all()):
if element.name == 'p':
if len(element.text.split()) > 2:
element.replace_with(BeautifulSoup(get_colorized_p(element), 'html.parser'))
將您的soup背部轉換為字串并嘗試顯示它:
display(HTML(str(soup)))
在較新的代碼中,請避免使用舊語法findAll(),而是使用find_all()- 更多請花一分鐘時間檢查檔案
例子
from bs4 import BeautifulSoup
from IPython.display import display, HTML
html = '''
<p>Elephants are the largest ...</p>
'''
soup = BeautifulSoup(html, 'html.parser')
def get_colorized_p(element):
### processing and returning of result str
return '<p><mark style="background: #edf8fb;">Elephants are the largest existing land animals.</mark><mark style="background: #f1fafc;">Three living species are currently recognised: the African bush elephant, the African forest elephant, and the Asian elephant.</mark><mark style="background: #f3fafc;">They are an informal grouping within the proboscidean family Elephantidae.</mark><mark style="background: #f3fafc;">Elephantidae is the only surviving family of proboscideans; extinct members include the mastodons.</mark><mark style="background: #eff9fb;">Elephantidae also contains several extinct groups, including the mammoths and straight-tusked elephants.</mark><mark style="background: #68c3a6;">African elephants have larger ears and concave backs, whereas Asian elephants have smaller ears, and convex or level backs.</mark><mark style="background: #56ba91;">The distinctive features of all elephants include a long proboscis called a trunk, tusks, large ear flaps, massive legs, and tough but sensitive skin.</mark><mark style="background: #d4efec;">The trunk is used for breathing, bringing food and water to the mouth, and grasping objects.</mark><mark style="background: #e7f6f9;">Tusks, which are derived from the incisor teeth, serve both as weapons and as tools for moving objects and digging.</mark><mark style="background: #d9f1f0;">The large ear flaps assist in maintaining a constant body temperature as well as in communication.</mark><mark style="background: #e5f5f9;">The pillar-like legs carry their great weight.</mark><mark style="background: #72c7ad;"> </mark></p>'
for element in soup.find_all():
if element.name == 'p':
if len(element.text.split()) > 2:
element.replace_with(BeautifulSoup(get_colorized_p(element), 'html.parser'))
display(HTML(str(soup)))
不完全相同,但非常接近您問題中的行為:如何使用 <b> 將每個單詞的首字母包裝在特定標簽中?
uj5u.com熱心網友回復:
element = get_colorized_p(element)分配一個區域變數,然后永遠不會被for回圈變數再次使用/覆寫。您需要保存已處理的元素,例如將它們連接成一個字串。
html = ''
for element in tqdm_notebook(soup.findAll()):
if element.name == 'p' and len(element.text.split()) > 2:
html = get_colorized_p(element)
else:
html = element.text
display(HTML(html))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483225.html
