从一组 HTML 文件中提取表格内容的最佳方法是什么?

发布于 2024-07-05 19:17:35 字数 50 浏览 6 评论 0原文

使用 TIDY 清理充满 HTML 文件的文件夹后,如何提取表格内容以进行进一步处理?

After cleaning a folder full of HTML files with TIDY, how can the tables content be extracted for further processing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光下的泡沫是彩色的 2024-07-12 19:17:35

在查看了这些建议后,我最终使用了 HtmlUnit

使用 HtmlUnit,我能够自定义 Java 代码来打开文件夹中的每个 HTML 文件,导航到 TABLE 标签,
查询每列内容并提取创建 CSV 文件所需的数据。

After reviewing the suggestions, I wound up using HtmlUnit.

With HtmlUnit, I was able to customize the Java code to open each HTML file in the folder, navigate to the TABLE tag,
query each column content and extract the data I needed to create a CSV file.

凯凯我们等你回来 2024-07-12 19:17:35

我过去曾使用 BeautifulSoup 来做这样的事情,并取得了巨大的成功。

I've used BeautifulSoup for such things in the past with great success.

我是有多爱你 2024-07-12 19:17:35

取决于您想要进行哪种处理。 您可以告诉 Tidy 生成 XHTML,这是 XML 的一种类型,这意味着您可以在结果上使用所有常用的 XML 工具,例如 XSLT 和 XQuery。

如果您想在 Microsoft Excel 中处理它们,那么您应该能够将表格从 HTML 中分割出来并将其放入文件中,然后在 Excel 中打开该文件:它会很乐意将 HTML 表格转换为电子表格页面。 然后,您可以将其保存为 CSV 或 Excel 工作簿等。(您甚至可以在 Web 服务器上使用它 - 返回 HTML 表格,但将 Content-Type 标头设置为 application/ ms-vnd.excel:Excel 将打开并导入表格,然后将其转换为电子表格。)

如果您希望将 CSV 输入数据库,那么您可以像以前一样通过 Excel,或者如果您想为了自动化该过程,您可以编写一个程序,使用您选择的 XML 导航 API 来迭代表行并将它们保存为 CSV。 Python 的 Elementtree 和 CSV 模块将使这变得非常简单。

Depends on what sort of processing you want to do. You can tell Tidy to generate XHTML, which is a type of XML, which means you can use all the usual XML tools like XSLT and XQuery on the results.

If you want to process them in Microsoft Excel, then you should be able to slice the table out of the HTML and put it in a file, then open that file in Excel: it will happily convert an HTML table in to a spreadsheet page. You could then save it as CSV or as an Excel workbook etc. (You can even use this on a web server -- return an HTML table but set the Content-Type header to application/ms-vnd.excel: Excel will open and import the table and turn it in to a spreadsheet.)

If you want CSV to feed in to a database then you could go via Excel as before, or if you want to automate the process, you could write a program that uses the XML-navigating API of your choice to iterate of the table rows and save them as CSV. Python's Elementtree and CSV modules would make this pretty easy.

奈何桥上唱咆哮 2024-07-12 19:17:35

如果您想从 HTML 标记中提取内容,您应该使用某种类型的 HTML 解析器。 为此,有很多工具,这里有两个可能适合您的需求:

http://jtidy.sourceforge.net /
http://htmlparser.sourceforge.net/

If you want to extract the content from the the HTML markup, you should use some type of HTML parser. To that end there are plenty out there and here are two that might suite your needs:

http://jtidy.sourceforge.net/
http://htmlparser.sourceforge.net/

听,心雨的声音 2024-07-12 19:17:35

遍历文本并使用正则表达式:)

http://www.knowledgehouse.sg

iterate through the text and Use regular expression :)

http://www.knowledgehouse.sg

浊酒尽余欢 2024-07-12 19:17:35

在 .NET 中,您可以使用 HTMLAgilityPack

请参阅 StackOverflow 上的上一个问题了解更多信息。

In .NET you could use HTMLAgilityPack.

See this previous question on StackOverflow for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文