从 jupyter-notebook 下载 HTML 文件到本地

Download HTML file from jupyter-notebook to local

如何使用给定的弹出窗口从 jupyter-notebook 下载文件 link?我需要这个弹出窗口 link 并单击它以将其下载为 HTML 文件。我遇到字符 " 双引号和 ' 的问题,这些字符给我的 HTML 代码带来了麻烦,以使其成为下载的弹出窗口。但是,我需要这些字符将其保留在我的脚本中而不用任何替换。

这是我的 HTML 脚本:

<html>
<head><meta charset='utf-8' /></head>
<body>
<h3>You can "view" HTML code "in" notebooks. Result 'want to' get it '"' testing only</h3>
</body>
</html>

这是我的代码python脚本:

from IPython.display import FileLink, HTML

title = "Download HTML file"
filename = "data.html"

payload = open("./cobaan_html2.html").read()
payload = payload.replace('<meta charset="utf-8" />', "<meta charset='utf-8' />")
html = '<a download="{filename}" href="data:text/html;charset=utf-8,'+payload+'" target="_blank">{title}</a>'

print(payload)
HTML(html)

这就是我得到的。

<html>
<head><meta charset="utf-8" /></head>
<body>
<h3>You can "view" HTML code "in" notebooks. Result 'want to' get it '"' testing only</h3>
</body>
</html>
You can "view" HTML code "in" notebooks. Result 'want to' get it '"' testing only
" target="_blank">{title}

以图像结果证明:

尝试将引号替换为不间断的 HTML 等价物,如下所示:

  1. " --> &quot;
  2. ' --> &apos;