简体   繁体   English

使用 C 解析 html

[英]Parse html using C

I need to grab some content from an HTML (XHTML valid) page.我需要从 HTML(XHTML 有效)页面中获取一些内容。 I grab the page using curl and store it in memory.我使用 curl 抓取页面并将其存储在内存中。

I played with the idea of using regex with the PCRE library, but simply I couldn't find any examples using it with C. Then I moved on to look at HTML parsers and again there is not a good selection.我尝试了在 PCRE 库中使用正则表达式的想法,但我找不到任何将它与 C 一起使用的示例。然后我继续查看 HTML 解析器,但再次没有一个好的选择。 All I could find was a skimpy documented module for libxml called HTMLparser.我所能找到的只是一个名为 HTMLparser 的 libxml 文档模块。

Are there any alternatives?有没有其他选择? If not, then examples for what I found already?如果没有,那么我已经找到的例子?

You want to use HTML tidy to do this.你想使用 HTML tidy 来做到这一点。 The Lib curl page has some source code to get you going. Lib curl 页面有一些源代码可以帮助您。 Documents traversing the dom tree.遍历 dom 树的文档。 You don't need an xml parser.您不需要 xml 解析器。 Doesn't fail on badly formated html.在格式错误的 html 上不会失败。

http://curl.haxx.se/libcurl/c/htmltidy.html http://curl.haxx.se/libcurl/c/htmltidy.html

I would use libhtmltidy + whatever xml parser like expat or libxml .我会使用libhtmltidy + 任何 xml 解析器,如expatlibxml Depends on what you're looking for.取决于你在寻找什么。

If you want to parse XML using C, then by far the best way to proceed is to use the LibXML library.如果您想使用 C 解析 XML,那么目前最好的方法是使用 LibXML 库。 The main page is at http://xmlsoft.org/ .主页位于http://xmlsoft.org/ In addition to their downloads, they have explicit code examples that specfically show how to handle parsing .除了他们的下载之外,他们还有明确的代码示例,具体展示了如何处理解析 I know for a fact you can get versions precompiled for Mac and Windows, most Linux and BSD distributions have it already included, and you can build from source if you wish.我知道你可以获得为 Mac 和 Windows 预编译的版本,大多数 Linux 和 BSD 发行版已经包含它,如果你愿意,你可以从源代码构建。

Google recently created a pure C99 library for parsing HTML, HTML5 specifically.谷歌最近创建了一个纯 C99 库来解析 HTML,特别是 HTML5。 It's easy to use in any C program and actively developed.它很容易在任何 C 程序中使用并积极开发。

https://github.com/google/gumbo-parser https://github.com/google/gumbo-parser

Fast C/C++ HTML 5 Parser.快速 C/C++ HTML 5 解析器。 Using threads.使用线程。 https://github.com/lexborisov/myhtml https://github.com/lexborisov/myhtml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM