使用 Python 编辑和创建 HTML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35355225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:20:18  来源:igfitidea点击:

Edit and create HTML file using Python

pythonhtmlpython-3.xpython-import

提问by Lilly123

I am really new to Python. I am currently working on an assignment for creating an HTML file using python. I understand how to read an HTML file into python and then edit and save it.

我对 Python 真的很陌生。我目前正在处理使用 python 创建 HTML 文件的作业。我了解如何将 HTML 文件读入 python,然后编辑并保存它。

table_file = open('abhi.html', 'w')
table_file.write('<!DOCTYPE html><html><body>')
table_file.close()

The problem with the above piece is it's just replacing the whole HTML file and putting the string inside write(). How can I edit the file and the same time keep it's content intact. I mean, writing something like this, but inside the body tags

上述部分的问题在于它只是替换了整个 HTML 文件并将字符串放入 write() 中。如何编辑文件并同时保持其内容完整。我的意思是,写这样的东西,但是在body 标签里面

<link rel="icon" type="image/png" href="img/tor.png">

I need the link to automatically go in between the opening and closing body tags.

我需要链接自动进入开始和结束正文标签之间。

采纳答案by Hugh Bothwell

You probably want to read up on BeautifulSoup:

您可能想阅读 BeautifulSoup

import bs4

# load the file
with open("existing_file.html") as inf:
    txt = inf.read()
    soup = bs4.BeautifulSoup(txt)

# create new link
new_link = soup.new_tag("link", rel="icon", type="image/png", href="img/tor.png")
# insert it into the document
soup.head.append(new_link)

# save the file again
with open("existing_file.html", "w") as outf:
    outf.write(str(soup))

Given a file like

给定一个文件,如

<html>
<head>
  <title>Test</title>
</head>
<body>
  <p>What's up, Doc?</p>
</body>
</html>  

this produces

这产生

<html>
<head>
<title>Test</title>
<link href="img/tor.png" rel="icon" type="image/png"/></head>
<body>
<p>What's up, Doc?</p>
</body>
</html> 

(note: it has munched the whitespace, but gotten the html structure correct).

(注意:它已经咀嚼了空白,但得到了正确的 html 结构)。

回答by Selcuk

You are using write (w) mode which will erase the existing file (https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files). Use append (a) mode instead:

您正在使用 write ( w) 模式,这将删除现有文件 ( https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files)。改用 append ( a) 模式:

table_file = open('abhi.html', 'a')