Html to Txt in C++

  
October 21

Html to Txt in C++

convert html to txt
html parser
C++;
本程序可以用來處理spider爬下來的網頁,提取其文本,去除html的tag;
本程序用於批量轉換某個文件夾下面所有的html文件;
 
程序總共分爲2部分,一部分是列出該文件夾下所有的文件(用C++遍歷一個文件夾下面所有的文件 Zz)
另一方面是將html轉換爲txt
 
 
  1. October 21
  2. Html to Txt in C++
  3. convert html to txt
  4. html parser
  5. C++;
  6. 本程序可以用來處理spider爬下來的網頁,提取其文本,去除html的tag;
  7. 本程序用於批量轉換某個文件夾下面所有的html文件;

  8. 程序總共分爲2部分,一部分是列出該文件夾下所有的文件(用C++遍歷一個文件夾下面所有的文件 Zz)
  9. 另一方面是將html轉換爲txt

  10. 點擊此處下載完整版本



  11. --------------------------------------------------------------------------------

  12. /*
  13. This code is to convert all html documents under c:/WT2G/ into text documents
  14.   Reference class ffsco and Html2txt codes;
  15.   By Super.jiju
  16.   [email protected]
  17.   super-jiju.spaces.live.com
  18.   Oct 22,2008;
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <windows.h>
  23. #include <locale.h>
  24. #include <string>
  25. #include "ffsco.h"
  26. void UnicodeToGB2312(char* pOut,unsigned short uData)
  27. {
  28.  WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
  29.  return;
  30. }

  31. #define BUFSIZE 1024*1024*2
  32. char buf[1024*1024*20];
  33. char shadowbuf[1024*1024*20];
  34. char buffer[BUFSIZE];
  35. long size;
  36. int type=0;
  37. FILE* fin=0,*fout=0;
  38. #define tocsize 14
  39. char* toc[tocsize]={" "," ","<","<",">",">",""","/"","&","&","©","◎版權","®","◎註冊"};
  40. //------list all file under the special folder
  41. typedef helper_coffs::ffsco::typeT filesc;
  42. filesc getfile(string mypath)
  43. {
  44.  helper_coffs::ffsco myfile;
  45.  myfile.dirs(1);
  46.  myfile.find(mypath);
  47.  filesc  content;
  48.  content=myfile.co_file();
  49.  return content;
  50. }
  51. //strstr快速比較
  52. int ministrstr(char* s,char* f)
  53. {
  54.  char minibuf[16];
  55.  memcpy(minibuf,s,15);
  56.  minibuf[15]=0;
  57.  return strstr(minibuf,f)-minibuf;
  58. };

  59. //strstr轉換爲小寫快速比較。
  60. int ministrstri(char* s,char *f)
  61. {
  62.  char minibuf[16];
  63.  memcpy(minibuf,s,15);
  64.  minibuf[15]=0;
  65.  strlwr(minibuf);
  66.  return strstr(minibuf,f)-minibuf;
  67. }
  68. // 等標記轉換
  69. int isintoc(char* streamstart)
  70. {
  71.  int i=0;
  72.  int ret=0;
  73.  while(i<tocsize)
  74.  {
  75.   if(!ministrstr(streamstart,toc[i]))
  76.   {
  77.     fprintf(fout,"%s",toc[i+1]);
  78.    ret=strlen(toc[i]);
  79.    break;
  80.   };
  81.   i+=2;
  82.  };
  83.  if(ret==0) //沒有轉換處理
  84.  {
  85.   fprintf(fout,"&");
  86.   ret=1;
  87.  };
  88.  return ret;
  89. };


  90. int num2txt(char* numstart)
  91. {
  92.  char tmp[256];
  93.  int pos=0;
  94.  char* s=numstart;
  95.  unsigned short word;
  96.  char os[3];
  97.  while( *s>='0' && *s <='9' )
  98.  {
  99.   tmp[pos++]=*s++;
  100.  };
  101.  tmp[pos]=0;
  102.  word=atoi(tmp);
  103.  memset(os,0,3);
  104.  UnicodeToGB2312(os,word);
  105.  fprintf(fout,"%s",os);
  106.  //s是;跳過
  107.  s++;
  108.  return  s-numstart;
  109. }

  110. //文件全部進入緩存
  111. void read2buf(FILE* fp)
  112. {
  113.  buf[0]=0;
  114.  size=0;
  115.  while(!feof(fp))
  116.  {
  117.   fgets(buffer,sizeof(buffer),fp);
  118.   strcat(buf+size,buffer);
  119.   size+=strlen(buffer);
  120.  };
  121.  buf[size]=0;
  122.  memcpy(shadowbuf,buf,size+1);
  123.  strlwr(shadowbuf);
  124. };

  125. //找標記的開始位置。返回找到後'>'之後的第一個字符位置。
  126. int findstart(char* start,int jump)
  127. {
  128.  char* pos=shadowbuf;
  129.  strlwr(start);
  130.  do
  131.  {
  132.   pos=strstr(pos,start);
  133.   if(pos-shadowbuf < 0 )return -1;
  134.   pos++;
  135.  }while(jump--);
  136.  while(*pos++ != '>')
  137.  {};
  138.  return pos-shadowbuf;
  139. };
  140. //找標記的結束位置。返回找到後'<'之前的最後字符位置。
  141. int findend(char* end,int start)
  142. {
  143.  char* pos=shadowbuf+start;
  144.  strlwr(end);
  145.  pos=strstr(pos,end);
  146.  if(pos-shadowbuf<0)return -1;
  147.  while(*pos-- != '<')
  148.  {};
  149.  return pos-shadowbuf;
  150. };

  151. void printline()
  152. {
  153.  switch(type)
  154.  {
  155.   case 1:
  156.   fprintf(fout,"%c",'/r');
  157.   break;
  158.   case 2:
  159.   fprintf(fout,"%s","/r/n");
  160.   break;
  161.   case 3:
  162.   fprintf(fout,"%c",'/n');
  163.   break;
  164.   default:
  165.   break;
  166.  };
  167. };

  168. //轉換輸出
  169. void h2t(char* s,int len)
  170. {
  171.  char* ss=s;
  172.  while(ss-s<len)
  173.  {
  174.   //判斷一下文章換行符號類型
  175.   if(type==0 && ( *ss=='/r'|| *ss=='/n'))
  176.   {
  177.    if(*ss=='/r' &&*(ss+1)=='/n')
  178.    {
  179.     type=2;
  180.    }else if(*ss=='/n')
  181.    {
  182.     type=3;
  183.    }else
  184.    {
  185.     type=1;
  186.    };
  187.   };
  188.   if(*ss!='<')
  189.   {//非標記
  190.    if(*ss=='&')
  191.    {
  192.     if(*(ss+1)=='#')
  193.     {
  194.      ss+=2;
  195.      int may=num2txt(ss);
  196.       ss+=may;
  197.     }else
  198.     {
  199.      int may=isintoc(ss);
  200.      if(may>0)
  201.      {
  202.       ss+=may;
  203.      };
  204.     }
  205.    }
  206.       else
  207.    {
  208.     fprintf(fout,"%c",*ss);
  209.     ss++;
  210.    };
  211.   }
  212.   else
  213.   {
  214.    //<script標記
  215.    if(!ministrstri(ss,"<script"))
  216.    {
  217.     ss++;
  218.     findnext:
  219.     while(*ss!='<' && ss-s <len)
  220.     {
  221.      ss++;
  222.     };

  223.     if(ss-s>=len)break;
  224.     while(ministrstri(ss,"</script")!=0 && ss-s<len)
  225.     {
  226.      ss++;
  227.      goto findnext;
  228.     };
  229.     if(ss-s>=len)break;
  230.     while(*ss!='>')ss++;
  231.     ss++;
  232.    }else if(!ministrstri(ss,"<style")) //<style標記
  233.    {
  234.     ss++;
  235.     findnext2:
  236.     while(*ss!='<' && ss-s <len)
  237.     {
  238.      ss++;
  239.     };
  240.     if(ss-s>=len)break;
  241.     while(ministrstri(ss,"</style")!=0 && ss-s<len)
  242.     {
  243.      ss++;
  244.      goto findnext2;
  245.     };
  246.     if(ss-s>=len)break;
  247.     while(*ss!='>')ss++;
  248.     ss++;
  249.    }else if(!ministrstri(ss,"</br>"))
  250.    {
  251.    // printline();
  252.     ss+=5;
  253.    }else if(!ministrstri(ss,"</p>"))
  254.    {
  255.  //   printline();
  256.    ss+=4;
  257.    }else if(!ministrstri(ss,"<br>"))
  258.    {
  259.    // printline();
  260.     ss+=4;
  261.    }
  262.    else //普通標記
  263.    {
  264.     while(*ss!='>' && ss-s<len)
  265.     {
  266.      ss++;
  267.     };
  268.     if(ss-s>=len)break;
  269.     ss++;
  270.    };
  271.   };
  272.  };
  273. };

  274. int main(void)
  275. {
  276.  std::string mypath="C://WT2G//";
  277.  filesc content=getfile(mypath);

  278.  for (helper_coffs::ffsco::typeT::iterator it = content.begin(); content.end() != it; it ++)
  279.  {
  280.    std::string filename;
  281.    filename=*it;
  282.    std::string writefile=filename+".txt";
  283.    fin=fopen(filename.c_str(),"r");
  284.    fout=fopen(writefile.c_str(),"w");
  285.    if(!fin)
  286.    {
  287.     exit(0);
  288.    };
  289.    read2buf(fin);
  290.    h2t(buf,size);
  291.    fclose(fin);
  292.    fclose(fout);
  293.  }
  294.  system("pause");
  295.  return 0;
  296. }

發佈了53 篇原創文章 · 獲贊 4 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章