使用Python编辑和创建HTML文件

Lil*_*123 9 html python python-import python-3.x

我是Python新手.我目前正致力于使用python创建HTML文件的任务.我理解如何将HTML文件读入python,然后编辑并保存.

table_file = open('abhi.html', 'w')
table_file.write('<!DOCTYPE html><html><body>')
table_file.close()
Run Code Online (Sandbox Code Playgroud)

上面这篇文章的问题是它只是替换整个HTML文件并将字符串放在write()中.如何编辑文件,同时保持其内容不变.我的意思是,写这样的东西,但在身体标签内

<link rel="icon" type="image/png" href="img/tor.png">
Run Code Online (Sandbox Code Playgroud)

我需要链接自动进入开始和结束的身体标签之间.

Hug*_*ell 19

你可能想读一下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))
Run Code Online (Sandbox Code Playgroud)

给出一个类似的文件

<html>
<head>
  <title>Test</title>
</head>
<body>
  <p>What's up, Doc?</p>
</body>
</html>  
Run Code Online (Sandbox Code Playgroud)

这会产生

<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> 
Run Code Online (Sandbox Code Playgroud)

(注意:它已经对空白进行了修改,但得到的html结构正确).