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,'<p>',chr(13)+chr(10),[rfReplaceAll,rfIgnoreCase]);
  aHtml:=DelStrbyTag(aHtml,'<script','</script>');
  aHtml:=StringReplace(aHtml,#$D#$A, '',[rfReplaceAll,rfIgnoreCase]); //回车换行符 ;
  aHtml:=StringReplace(aHtml,'&nbsp;','',[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,'&ldquo;','“',[rfReplaceAll,rfIgnoreCase]);
  s:=StringReplace(s,'&rdquo;','”',[rfReplaceAll,rfIgnoreCase]);
// s:=StringReplace(s,' ','',[rfReplaceAll,rfIgnoreCase]);
  s:=StringReplace(s,' ','',[rfReplaceAll,rfIgnoreCase]);
  Result:=s;
end;

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

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

相关阅读 >>

Delphi setcurrentdir 设置当前文件夹路径

Delphi 内存加载并运行exe程序

Delphi+access错误"不正常地定义参数对象。提供了不一致或不完整的信息。"

Delphi webbrowser1 读取当前页面字体大小

Delphi获取进程和模块信息

Delphi图像二值化

Delphi 将整数转换为罗马数字(字符串)2

Delphi tms web core messagedlg对话框 yes no

Delphi的listview自动排序

Delphi app检测智能手机震动

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...