Parse html using C

2019-01-14 17:17发布

I need to grab some content from an HTML (XHTML valid) page. I grab the page using curl and store it in memory.

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. All I could find was a skimpy documented module for libxml called HTMLparser.

Are there any alternatives? If not, then examples for what I found already?

4条回答
霸刀☆藐视天下
2楼-- · 2019-01-14 17:30

If you want to parse XML using C, then by far the best way to proceed is to use the LibXML library. The main page is at 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.

查看更多
别忘想泡老子
3楼-- · 2019-01-14 17:33

You want to use HTML tidy to do this. The Lib curl page has some source code to get you going. Documents traversing the dom tree. You don't need an xml parser. Doesn't fail on badly formated html.

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

查看更多
贪生不怕死
4楼-- · 2019-01-14 17:34

Google recently created a pure C99 library for parsing HTML, HTML5 specifically. It's easy to use in any C program and actively developed.

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

查看更多
成全新的幸福
5楼-- · 2019-01-14 17:49

I would use libhtmltidy + whatever xml parser like expat or libxml. Depends on what you're looking for.

查看更多
登录 后发表回答