如何用C语言实现解析HTML文档

Python012

如何用C语言实现解析HTML文档,第1张

参考下面代码:

#include <stdio.h>

#include <streamhtmlparser/htmlparser.h>

int main(void)

{

unsigned int getchar_ret

htmlparser_ctx *parser = htmlparser_new()

while ((getchar_ret = getchar()) != EOF) {

char c = (char)getchar_ret

/* If we received a '$' character, we output the current tag and attribute

* * name to stdout. */

if (c == '$') {

printf("[[ ")

if (htmlparser_tag(parser))printf("tag=%s ", htmlparser_tag(parser))

if (htmlparser_attr(parser)) printf("attr=%s ", htmlparser_attr(parser))

printf("]]")

/* If we read any other character, we pass it to the parser and echo it to

* * stdout. */

} else {

htmlparser_parse_chr(parser, c)

putchar(c)

}

}

}

1. 首先定义一个File 的变量

2.然后用

FILE *fopen(

const char *filename,

const char *mode

)

这个函数读入 html 文件

3.用这个读入文件内容

size_t fread(

void *buffer,

size_t size,

size_t count,

FILE *stream

)

4.之后进行字符数组的处理