如何在Python中写入HTML中的特定位置?

时间:2018-01-14 16:06:09

标签: python

我有一个大型HTML文件,我想写入HTML文件中的特定位置,但我不想在原始文件上写,这就是为什么我要创建它的副本。目前,我有一个代码如下:

import shutil

shutil.copy2('./file.html', './new_file.html')
f = open('new_file.html', 'w') 
content = '<tr><td>New value<td></tr>'
# TODO: I need to write the above content after the <tbody> tag in HTML,
# please note that I have just one <tbody> tag in my HTML file
f.close()

如何查找如何查找我的HTML中content标记之后的tbody变量?

2 个答案:

答案 0 :(得分:1)

有很多不同的方法来解决这个问题。一种选择是使用模板,即您搜索和替换的HTML文件中的某些已知值。 Python的常见模板引擎是jinja2。这是一个例子:

HTML文件:

<html>
<head></head>
<body>
{{ replace_me }}
</body>
</html>

现在是Python代码:

from jinja2 import Template
with open('file.html') as f:
    t = Template(f.read())

# Create a dict with template keys and their values
vals = {'replace_me': 'some new text'}

with open('new_file.html') as f:
    f.write(t.render(vals))

呈现的HTML:

<html>
<head></head>
<body>
some new text
</body>
</html>

答案 1 :(得分:0)

这是使用python的默认数据类型(没有模板,附加库等)的基本方法:

#!/usr/bin/env python3
import shutil

f = open('file.html', 'r')
old_file_content = f.read()
f.close()

# print the file that was read
print(old_file_content)

replacee = '<td>Old value<td>'
replacement = '<td>New value<td>'
new_file_cntent = old_file_content.replace(replacee, replacement)

# print the modified content, for comparison
print(new_file_cntent)

这是输出:

mindaugasb@python:~/workspace/StackOverflow $ ./insert_text_to_html.py 
<!DOCTYPE html>
<html>
    <head>
        <title>Test title!</title>
    </head>
    <body>
        <h1>My First Heading</h1>
        <table>
            <tr>
                <th>Foo</th>
                <th>Bar</th>
            </tr>
            <tr>
                <td>Old value<td> <--- The part that will be replaced
                <td>W00t!</td>
            </tr>
        </table>
        <p>My first paragraph.</p>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <title>Test title!</title>
    </head>
    <body>
        <h1>My First Heading</h1>
        <table>
            <tr>
                <th>Foo</th>
                <th>Bar</th>
            </tr>
            <tr>
                <td>New value<td> <-- the part that replaced the old part
                <td>W00t!</td>
            </tr>
        </table>
        <p>My first paragraph.</p>
    </body>
</html>

在此之后,将修改后的内容添加到新文件或做任何听到的事情并不困难。