鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > delphi > >

Delphi7的HtmlParser使用方法

来源:互联网 作者:佚名 时间:2017-09-02 12:30
uses HtmlParserprocedure TForm4.Button1Click(Sender: TObject);var FNodes:IHtmlElement; aString:string;begin FNodes := parserHtml(Memo1.Text); aString:= FNodes.Children[2].Children[1].Children[5].Children[0].InnerText;end; // 判断某子结点是

uses
  HtmlParser

procedure TForm4.Button1Click(Sender: TObject);
var FNodes:IHtmlElement;
   aString:string;
begin
  FNodes := parserHtml(Memo1.Text);
 aString:= FNodes.Children[2].Children[1].Children[5].Children[0].InnerText;
end;
//判断某子结点是否存在,增加代码的可靠性
//if NodeIsExist(FNodes,
//'Children[2].Children[3].Children[11].Children[3].Children[1].Children[1].Children[1].Children[0]'
//)  then
//Memo3.Lines.Add(
//FNodes.Children[2].Children[3].Children[11].Children[3].Children[1].Children[1].Children[1].Children[0].InnerText
// );
  function NodeIsExist(Root:IHtmlElement;Childrens:string):Boolean ;
  var aStringlist:tStringlist;   i,aIndex:integer;
      aNode:IHtmlElement;   aStr:string;
  begin
    Result:= False;
    aStringlist:=TStringList.Create;
  aStringlist.Delimiter := '.';
  aStringlist.DelimitedText := Childrens;

  aNode:=Root;
  for i := 0 to aStringlist.Count-1 do  begin
    aStr:=Copy(aStringlist[i],10,Length(aStringlist[i])-10);
    aIndex:= StrToIntDef(aStr,0);
   if aIndex>=  aNode.ChildrenCount  then  begin
    aStringlist.Free;
    Exit;
   end;
     aNode:=aNode.Children[aIndex];
  end;
   aStringlist.Free;
   Result:= true;
  end;

      HtmlParser例子

 //NodeIsExist的优化版,更高速,只处理最大结点0~99
 function NodeIsExist(Root:IHtmlElement;Childrens:string):Boolean ;
  VAR i:integer;
   aNode:IHtmlElement;
  begin
     Result:= False;
     aNode:=Root;
     for i:=1 to Length(Childrens) do    begin //aString[1]从1开始,而不是从零开始
       if Childrens[i]=#93 then begin  //  ']'
           if Childrens[i-2]=#91 then    begin    //  '['
           if StrToIntDef(Copy(Childrens,i-1,1),0)>=  aNode.ChildrenCount  then   Exit;  //一位数
           aNode:=aNode.Children[StrToIntDef(Copy(Childrens,i-1,1),0)];
           end;
           if Childrens[i-3]=#91 then    begin //  '['
           if StrToIntDef(Copy(Childrens,i-2,2),0)>=  aNode.ChildrenCount  then   Exit;  //二位数
           aNode:=aNode.Children[StrToIntDef(Copy(Childrens,i-2,2),0)];
           end;
       end;

     end;
    Result:= true;
  end;

 

网友评论
<