Delphi7的HtmlParser使用方法

Delphi7的HtmlParser使用方法

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;

 

发表于 2016-12-08 21:06  涂磊  阅读(1532)  评论(0)  编辑  收藏
上一篇:flutter 基于掘金App 还原的UI页面


下一篇:Delphi - Delphi7 调用阿里大于实现短信消息验证