delphi 提取html网页源文件纯文本函数可做采集


本文整理自网络,侵删。

 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>0and (aBk>0then
    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]); //回车换行符 ; www.delphitop.com
  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 xe10.2 firedac 三种连接

Delphi 安卓添加资源图片并读取

Delphi 2009 之 tcategorypanelgroup[3]: color

Delphi xe 取得 app 自己的版本号 (狠跨 4 个平台)

Delphi编写dll(以及静态和动态方式调用)

学习使用Delphi for android 调用java类库

Delphi randomize 随机数

Delphi toolbar按钮添加图标

Delphi 删除整个目录

Delphi判断按下的是回车键?

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



打赏

取消

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

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

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

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

评论

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

    正在狠努力加载,请稍候...