delphi 提取网页源文件纯文本函数  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 提取网页源文件纯文本函数


function GetHtmltxt(aHtml:string):string;

function DelStrbyTag(aHtml,aFtTag,aEndTag:string):string; // 除去制点 tag 之间的数据
var
  aFt,aBk:integer;
  TempStr,BackStr:string;
begin
  TempStr:=aHtml;
  BackStr:='';
  while Tempstr<>'' do
  begin
    aFt:=Pos(aFtTag,Tempstr);
    aBk:=Pos(aEndTag,Tempstr);
    if (aFt>0) and (aBk>0) then
    begin
      BackStr:=BackStr+copy(Tempstr,1,aFt-1);
      TempStr:=copy(TempStr,aBk+length(aEndTag),length(tempstr));
    end
    else
    begin
      BackStr:=BackStr+tempstr;
      tempstr:='';
    end;
  end;
  Result:=BackStr;
end;

var
  i:integer;
  s:string;
begin
  i:=1;
  s:='';
  aHtml:=trim(aHtml);
  aHtml:=stringReplace(aHtml,'

',chr(13)+chr(10),[rfReplaceAll,rfIgnoreCase]);
  aHtml:=DelStrbyTag(aHtml,'');
  aHtml:=StringReplace(aHtml,#$D#$A, '',[rfReplaceAll,rfIgnoreCase]); //回车换行符 ;
  aHtml:=StringReplace(aHtml,' ','',[rfReplaceAll,rfIgnoreCase]); //删除Html空格
  while i<=length(aHtml) do
  begin
    if aHtml[i]='<' then
     repeat inc(i)
    until (aHtml[i]='>')
    else
    begin
      if aHtml[i]<>' ' then
      begin
        s:=s+aHtml[i];
      end
      else
      begin
        if s[length(s)]<>' ' then
        begin
          s:=s+aHtml[i];
        end;
      end;
    end;
    inc(i);
  end;   
  s:=StringReplace(s,'“','“',[rfReplaceAll,rfIgnoreCase]);
  s:=StringReplace(s,'”','”',[rfReplaceAll,rfIgnoreCase]);
// s:=StringReplace(s,' ','',[rfReplaceAll,rfIgnoreCase]);
  s:=StringReplace(s,' ','',[rfReplaceAll,rfIgnoreCase]);
  Result:=s;
end;

说明:类似 (webbrowser1.Document as IHTMLDocument2 ).body.innertext;

这个功能,但自己写的可以控制。可以分段。

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.083789110183716 seconds