gpt4 book ai didi

javascript - 如何通过python在html文件中删除和添加新内容

转载 作者:行者123 更新时间:2023-11-30 19:01:52 25 4
gpt4 key购买 nike

我正在创建一个 HTML 文件,它从 highcharts.com 下载其内容。默认 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)

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

最佳答案

您可以使用标准 replace()替换 "</html>""some text </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>'''

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)

关于javascript - 如何通过python在html文件中删除和添加新内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59438635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com