如何从Google Colab中的HTML文件提取文本

时间:2020-10-07 20:09:08

标签: python html google-colaboratory

我写了一些HTML文件。我想在colab中将此用作数据(文本数据)。例如,我要使用<h1> "text "</h1>中的文本,还需要提取整个类和此类人员。您能告诉我如何在Google colab中上传HTML文件,以及如何从此处提取一些文本。 预先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用IPython.display.HTML助手来呈现HTML。

然后,使用eval_js中的google.colab.output帮助器来获取呈现的HTML中的文本。

这里是full example

enter image description here

在此处复制关键代码:

from IPython.display import HTML, display
from google.colab import output

# Render the HTML.
display(HTML(open('example.html').read()))

html_text = output.eval_js('document.body.innerText')

要上传本地文件,可以使用左侧文件浏览器面板,如下所示: enter image description here