用python替换xml或html上的标记名称

时间:2018-05-29 04:52:53

标签: python html xml

我想更改一些html标签名称。 让我知道python代码的方法。:

按原样

<div class="title-title-1">hello</div>
<div class="text-body">i like you</div>
<div class="p">hehe</div>

<title-title-1>hello</title-title-1>
<text-body>i like you</text-body>
<p>hehe</p>
有人帮助我!!

1 个答案:

答案 0 :(得分:0)

使用 BeautifulSoup

<强>演示:

maxConnections

<强>输出:

from bs4 import BeautifulSoup
s = """<div class="title-title-1">hello</div>
<div class="text-body">i like you</div>
<div class="p">hehe</div>"""
soup = BeautifulSoup(s, "html.parser")

title = soup.find("div", class_="title-title-1").text
body = soup.find("div", class_="text-body").text
p = soup.find("div", class_="p").text

toFrame = """<title-title-1>{0}</title-title-1>
<text-body>{1}</text-body>
<p>{2}</p>""".format(title, body, p)

print(toFrame)