【发布时间】:2021-09-06 20:27:50
【问题描述】:
我有一个 Python 代码,可以很好地解析 html 文件中的一些数据。在代码的末尾,我必须按标签保存 html 文件。例如,我有这 3 个带有 3 个标题标签的 html 文件:
<title>My name is Prince</title>
<title>I love Madonna</title>
<title>Cars and Candies</title>
它们中的每一个都必须像这样保存:
my-name-is-prince.html
I-love-madonna.html
cars-and-candies.html
所以,我已经有了一些 Python 的 SAVE 解决方案,但我不知道如何按标签保存。
try:
title = re.search('<title.+/title>', html)[0]
title_content = re.search('>(.+)<', title)[1]
except:
pass
with open("my-words.html", "w") as some_file_handle:
some_file_handle.write(finalString)
或
with open('page_323.txt', 'w') as f:
f.write(result.text)
或
with open("somefilename.txt", "w") as some_file_handle:
for line in data:
some_file_handle.write(line + "\n")
附:我有 500 个文件。 Python 代码必须从每个 html 中找到每个标签,并将它们中的每一个保存到新的 html 中。
【问题讨论】:
-
您是否已经从 html 中提取了标题?