【发布时间】:2012-03-15 15:29:36
【问题描述】:
我正在尝试创建一个简单的 c 程序,从网页中删除 HTML 并保留文本。到目前为止,我已经想出了下面的代码。它使用 cURL 获取网页的内容并将其写入文件。如何通过内存缓冲区并删除所有 HTML 标记并将文本输出到终端或文件?
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#define WEBPAGE_URL "http://homepages.paradise.net.nz/adrianfu/index.html"
#define DESTINATION_FILE "/home/acwest/data.txt"
size_t write_data( void *ptr, size_t size, size_t nmeb, void *stream)
{
return fwrite(ptr,size,nmeb,stream);
}
int main()
{
int in_tag = 0;
char * buffer;
char c;
long lSize;
size_t result;
FILE * file = fopen(DESTINATION_FILE,"w+");
if (file==NULL) {
fputs ("File error",stderr);
exit (1);
}
CURL *handle = curl_easy_init();
curl_easy_setopt(handle,CURLOPT_URL,WEBPAGE_URL); /*Using the http protocol*/
curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(handle,CURLOPT_WRITEDATA, file);
curl_easy_perform(handle);
curl_easy_cleanup(handle);
// obtain file size:
fseek (file, 0, SEEK_END);
lSize = ftell (file);
rewind (file);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {
fputs ("Memory error",stderr);
exit (2);
}
// copy the file into the buffer:
result = fread (buffer,1,lSize,file);
if (result != lSize) {
fputs ("Reading error",stderr);
exit (3);
}
}
【问题讨论】:
-
您可以使用现有的解析库,例如 expat.sourceforge.net
-
请注意:您尝试实现的目标将接近于 bash 脚本中的单行,使用 curl 和 sed 的组合。
-
@user667430:您的代码甚至无法编译……
-
甚至不要考虑用那种愚蠢的语法来解析 html。例如:你会得到所有的 javascript 和 css 代码。这就是为什么 sed 也是一个坏主意的原因,尽管我同意 curl+其他一些实用程序(html2txt 或类似的东西)这是一个单行。