どのようにして、Cを使用してHTML文書内の< ...>タグ間でHTMLを取り除くことができますか?私の現在のプログラムは、ウェブページの内容を取得するためにカールを使用し、それをテキストファイルに入れてから、テキストファイルから読み込み、<>を削除しますが、それらのタグ間のすべてを削除する方法はわかりません。 (あなたが他の宣言にネストつのタグを持っていないことを意味し、あなたが適切なHTMLを扱うことを想定し「<」と「>」タグの間に内容を削除するだけで、コードを配置Cストリップhtmlの間<...>
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define WEBPAGE_URL "http://homepages.paradise.net.nz/adrianfu/index.html"
#define DESTINATION_FILE "/home/user/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);
int i, nRead, fd;
int source;
char buf[1024];
if((fd = open("data.txt", O_RDONLY)) == -1)
{
printf("Cannot open the file");
}
else
{
nRead = read(fd, buf, 1024);
printf("Original String ");
for(i=0; i<nRead; i++)
{
printf("%c", buf[i]);
}
printf("\nReplaced String ");
for(i=0; i<nRead; i++)
{
if(buf[i]=='<' || buf[i]=='>'){
buf[i]=' ';
}
printf("%c", buf[i]);
}
}
close(source);
return 0;
}
を処理します。 '<'でカウンタをインクリメントし、 '>'でカウンタをインクリメントします。カウンタがゼロの場合のみ出力を行います。 (fencepostエラーに注意してください) – wildplasser