• 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;
  • 相关阅读:
    毕业设计:参考文献(3)
    毕业设计:参考文献(5)
    毕业设计:参考文献(1)
    毕业设计:参考文献(7)
    毕业设计:参考文献(2)
    2021ICPC沈阳站游记
    2021CCPC广州站游记
    常用linux 脚本
    Jenkins调优实践
    linux工具安装
  • 原文地址:https://www.cnblogs.com/tulater/p/6146487.html
Copyright © 2020-2023  润新知