我正在嘗試提取特定的 HTML 標簽并翻譯其中的文本值,并用翻譯后的值替換文本。我正在使用 python 和 bs4。我一直在考慮字串替換,但我不知道如何將BeautifulSoup作為字串處理。這是我提取的html代碼
<p style="text-align: center;">Everyone Active has opened its own online shop that’s packed with fantastic quality fitness equipment that’s perfect for helping you work out at home at incredible prices. Follow the link below to find out more.</p>
我希望最終結果是:
<p style="text-align: center;">Everyone Active ha abierto su propia tienda en línea que está repleta de equipos de fitness de calidad fantástica que son perfectos para ayudarte a hacer ejercicio en casa a precios increíbles. Sigue el enlace de abajo para descubrir mas.</p>
uj5u.com熱心網友回復:
用 replace_with()
這是您如何做到的。
from bs4 import BeautifulSoup
s= """
<p style="text-align: center;">Everyone Active has opened its own online shop that’s packed with fantastic quality fitness equipment that’s perfect for helping you work out at home at incredible prices. Follow the link below to find out more.</p>
"""
soup = BeautifulSoup(s, 'lxml')
p_tag = soup.find('p')
#Replacing the Text
p_tag.string.replace_with('Everyone Active ha abierto su propia tienda en línea que está repleta de equipos de fitness de calidad fantástica que son perfectos para ayudarte a hacer ejercicio en casa a precios increíbles. Sigue el enlace de abajo para descubrir mas.')
print(soup)
輸出:
<html>
<body>
<p style="text-align: center;">
Everyone Active ha abierto su propia tienda en línea que está repleta de equipos de fitness de calidad fantástica que son perfectos para ayudarte a hacer ejercicio en casa a precios increíbles. Sigue el enlace de abajo para descubrir mas.
</p>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/349832.html
