西游爱好者 發表於 2022-2-7 22:36:00

Delphi 解析HTML

<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">uses mshtml;IHTMLEleMent.ID;IHTMLEleMent.tagName;IHTMLEleMent.title;elmt._className;elmt.getAttribute('anchor', 0);procedure TForm1.btnphClick(Sender: TObject);
var
Document: IHTMLDocument2;
FTableCollection, tempCoc: IHTMLElementCollection;
table: IHTMLTABLE;
TableRow: IHTMLTableRow;
elmt: IHTMLEleMent;
I, J, K: integer;
str: string;
begin
Document := WebBrowser1.Document as IHTMLDocument2;
FTableCollection := Document.all;
FTableCollection.Length; //
FTableCollection.item(1, 0);
FTableCollection := Document.all.tags('table') as IHTMLElementCollection;
for I := 0 to FTableCollection.Length - 1 do
begin
    table := FTableCollection.item(I, 0) as IHTMLTABLE;//题数
    for J := 0 to table.rows.Length - 1 do
    begin
      TableRow := (table.rows.item(J, 0) as IHTMLTableRow); //每道题信息
      str := '';
      for K := 0 to TableRow.cells.Length - 1 do
      begin
      elmt := TableRow.cells.item(K, 0) as IHTMLEleMent;
      str := str + elmt.innerText + #9;
      end;
      str := StringReplace(str, ''#$D#$A'', '', );
      Memo3.Lines.Add(str);
      Memo3.Lines.Add('------------------------------------------');
    end;
end;
end;
</pre>
</div>
<p>  </p>
<p>&nbsp;URL := 'http://bbs.csdn.net/forums/Delphi';<br>&nbsp; WebBrowser1.Navigate(URL);</p>
<p>下面是一行tablerow的数据,可以分解出来,&lt;td&gt;&lt;td&gt;之间是一个cell列。</p>
<p>如何解析第一列的href、class之间的3个数据呢,?、delphi7 滚动条颜色、VCL组件开发及应用</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">tr</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="title"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">strong </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="green"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>?<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">strong</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/topics/390861446"</span><span style="color: rgba(255, 0, 0, 1)"> target</span><span style="color: rgba(0, 0, 255, 1)">="_blank"</span><span style="color: rgba(255, 0, 0, 1)"> title</span><span style="color: rgba(0, 0, 255, 1)">="delphi7 滚动条颜色"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>delphi7 滚动条颜色<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="forum_link"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>[<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/forums/Delphi"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Delphi<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/forums/DelphiVCL"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>VCL组件开发及应用<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>]<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tc"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>20<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tc"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="http://my.csdn.net/u010745617"</span><span style="color: rgba(255, 0, 0, 1)"> rel</span><span style="color: rgba(0, 0, 255, 1)">="nofollow"</span><span style="color: rgba(255, 0, 0, 1)"> target</span><span style="color: rgba(0, 0, 255, 1)">="_blank"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>u010745617<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;</span><span style="color: rgba(128, 0, 0, 1)">br </span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="time"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>08-15 16:25<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tc"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>1<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tc"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="http://my.csdn.net/NongCunGongLu"</span><span style="color: rgba(255, 0, 0, 1)"> rel</span><span style="color: rgba(0, 0, 255, 1)">="nofollow"</span><span style="color: rgba(255, 0, 0, 1)"> target</span><span style="color: rgba(0, 0, 255, 1)">="_blank"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>NongCunGongLu<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;</span><span style="color: rgba(128, 0, 0, 1)">br </span><span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="time"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>08-17 13:41<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="tc"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/topics/390861446/close"</span><span style="color: rgba(255, 0, 0, 1)"> target</span><span style="color: rgba(0, 0, 255, 1)">="_blank"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>管理<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">tr</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">td </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="title"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">strong </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="green"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>?<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">strong</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/topics/390861446"</span><span style="color: rgba(255, 0, 0, 1)"> target</span><span style="color: rgba(0, 0, 255, 1)">="_blank"</span><span style="color: rgba(255, 0, 0, 1)"> title</span><span style="color: rgba(0, 0, 255, 1)">="delphi7 滚动条颜色"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>delphi7 滚动条颜色<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="forum_link"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>[<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="parent"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/forums/Delphi"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Delphi<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">a </span><span style="color: rgba(255, 0, 0, 1)">href</span><span style="color: rgba(0, 0, 255, 1)">="/forums/DelphiVCL"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>VCL组件开发及应用<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>]<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">td</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>把td这一部分IHTMLEleMent当作IHTMLElementCollection解析就可以了,tagName,getAttribute('href',0),title,_className都可以获得正确的值,是6个集合元素。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">tempCoc := elmt.all as IHTMLElementCollection;
      if (tempCoc.Length = 6) then
      begin
          for q := 0 to tempCoc.Length - 1 do
          begin
            emt2 := tempCoc.item(q, 0) as IHTMLEleMent;
            if emt2.tagName = 'STRONG' then
            s2 := emt2.innerText
            else if emt2.tagName = 'A' then
            begin
            s2 := emt2.getAttribute('href',0);
            s2 := emt2.title;
            s2 := emt2.innerText;
            end
            else if emt2.tagName = 'SPAN' then
            begin
            emt2.tagName;
            emt2._className;
            emt2.title;
            emt2.innerText;
            end;
          end;
      end;</span></pre>
</div>
<p>lmt: IHTMLEleMent;</p>
<p>&nbsp; elmt := (WebBrowser1.Document as ihtmldocument3).getElementById('idbtn001');</p>
<p>getElementsByName</p>
<p>getElementById</p>
<p>getElementsByTagName</p>
<p>&nbsp;</p>
<p>doc2: IHTMLDocument2;<br>doc3: IHTMLDocument3;</p>
<p>&nbsp; doc2.forms.item('form1',0) as IHTMLFormElement;//取form1的表单</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>elmt := (WebBrowser1.Document <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLDocument3).getElementById(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">divfirstID</span><span style="color: rgba(128, 0, 0, 1)">'</span>);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过ID取得某个节点</span>
<span style="color: rgba(0, 0, 0, 1)">
elmt :</span>= ((elmt.children <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLElementCollection).item(<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>)) <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLEleMent;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">子节点</span>
elmt := ((elmt.children <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLElementCollection).item(<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>)) <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLEleMent;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">子节点</span>
elmt := ((elmt.children <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLElementCollection).item(<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>)) <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLEleMent;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">子节点</span>
elmt := ((elmt.children <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLElementCollection).item(<span style="color: rgba(128, 0, 128, 1)">2</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>)) <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLEleMent;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">子节点里的第3个子节点</span>
elmt := ((elmt.children <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLElementCollection).item(<span style="color: rgba(128, 0, 128, 1)">0</span>, <span style="color: rgba(128, 0, 128, 1)">0</span>)) <span style="color: rgba(0, 0, 255, 1)">as</span> IHTMLEleMent;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">子节点</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/bytebull/p/15869655.html
頁: [1]
查看完整版本: Delphi 解析HTML