【问题标题】:How to delete and add new contents in html file by python如何通过python在html文件中删除和添加新内容
【发布时间】:2019-12-21 18:07:42
【问题描述】:

我正在创建一个从 highcharts.com 下载其内容的 HTML 文件。默认 HTML 内容的头部如下所示

<head>
        <meta charset="utf-8" />
        <link href="https://www.highcharts.com/highslide/highslide.css" rel="stylesheet" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/highcharts.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/highcharts-more.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/modules/heatmap.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/modules/exporting.js"></script>
    </head>

我想将其替换为:

<head>
        <meta charset="utf-8" />
        <link href="https://www.highcharts.com/highslide/highslide.css" rel="stylesheet" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="https://code.highcharts.com/highcharts.js"></script>
        <script src="https://code.highcharts.com/modules/series-label.js"></script>
        <script src="https://code.highcharts.com/modules/exporting.js"></script>
        <script src="https://code.highcharts.com/modules/export-data.js"></script>
        <script src="https://code.highcharts.com/modules/accessibility.js"></script>

        <figure class="highcharts-figure">
            <div id="container"></div>
            <p class="highcharts-description">
                Basic line chart showing trends in a dataset. This chart includes the
                <code>series-label</code> module, which adds a label to each line for
                enhanced readability.
            </p>
        </figure>

    </head>

我正在使用下面的代码来编写来自 highcharts.com (H) 的 HTML 中的默认内容

# write out the html
FileName1 = "Highcharts_Test.html"
with open(FileName1, "wt") as fh:
    fh.write(H.htmlcontent)

我怎样才能实现上述目标?

【问题讨论】:

  • 如果你有它作为字符串,那么你可以使用标准的字符串函数,如text_html.replace("&lt;/head&gt;", "some_text &lt;/head&gt;")
  • 它不起作用。线条太长。 text_html.replace 是 python 库的一部分或者我需要导入 Html?
  • 它是标准函数 "Hello World".replace("Hello", "Goodbye") 并且您可以将变量与文本 "Hello World".replace(variable1, variable2) 一起使用,因此您可以使用三重 " - variable = """here text in many lines""" 分配多行文本,或者您可以从文件 @987654329 中读取此文本@

标签: javascript python html python-3.x highcharts


【解决方案1】:

您可以使用标准的replace()"&lt;/html&gt;" 替换为"some text &lt;/html&gt;"

html = '''<head>
        <meta charset="utf-8" />
        <link href="https://www.highcharts.com/highslide/highslide.css" rel="stylesheet" />
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/highcharts.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/highcharts-more.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/modules/heatmap.js"></script>
        <script type="text/javascript" src="https://code.highcharts.com/6/modules/exporting.js"></script>
</head>'''

new = '''
        <figure class="highcharts-figure">
            <div id="container"></div>
            <p class="highcharts-description">
                Basic line chart showing trends in a dataset. This chart includes the
                <code>series-label</code> module, which adds a label to each line for
                enhanced readability.
            </p>
        </figure>

</head>'''


html = html.replace('</head>', new)

print(html)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 2022-01-14
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多