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(, );
FTableCollection := Document.all.tags('table') as IHTMLElementCollection;
for I := to FTableCollection.Length - do
begin
table := FTableCollection.item(I, ) as IHTMLTABLE; //题数
for J := to table.rows.Length - do
begin
TableRow := (table.rows.item(J, ) as IHTMLTableRow); //每道题信息
str := '';
for K := to TableRow.cells.Length - do
begin
elmt := TableRow.cells.item(K, ) as IHTMLEleMent;
str := str + elmt.innerText + #;
end;
str := StringReplace(str, ''#$D#$A'', '', [rfReplaceAll]);
Memo3.Lines.Add(str);
Memo3.Lines.Add('------------------------------------------');
end;
end;
end;

URL := 'http://bbs.csdn.net/forums/Delphi';
  WebBrowser1.Navigate(URL);

下面是一行tablerow的数据,可以分解出来,<td><td>之间是一个cell列。

如何解析第一列的href、class之间的3个数据呢,?、delphi7 滚动条颜色、VCL组件开发及应用

          <tr>
<td class="title">
<strong class="green">?</strong>
<a href="/topics/390861446" target="_blank" title="delphi7 滚动条颜色">delphi7 滚动条颜色</a>
<span class="forum_link">[<span class="parent"><a href="/forums/Delphi">Delphi</a></span> <a href="/forums/DelphiVCL">VCL组件开发及应用</a>]</span>
</td>
<td class="tc">20</td>
<td class="tc">
<a href="http://my.csdn.net/u010745617" rel="nofollow" target="_blank">u010745617</a><br />
<span class="time">08-15 16:25</span></td>
<td class="tc">1</td>
<td class="tc">
<a href="http://my.csdn.net/NongCunGongLu" rel="nofollow" target="_blank">NongCunGongLu</a><br />
<span class="time">08-17 13:41</span>
</td>
<td class="tc">
<a href="/topics/390861446/close" target="_blank">管理</a>
</td>
</tr>
    <td class="title">
<strong class="green">?</strong>
<a href="/topics/390861446" target="_blank" title="delphi7 滚动条颜色">delphi7 滚动条颜色</a>
<span class="forum_link">[<span class="parent"><a href="/forums/Delphi">Delphi</a></span> <a href="/forums/DelphiVCL">VCL组件开发及应用</a>]</span>
</td>

把td这一部分IHTMLEleMent当作IHTMLElementCollection解析就可以了,tagName,getAttribute('href',0),title,_className都可以获得正确的值,是6个集合元素。

       tempCoc := elmt.all as IHTMLElementCollection;
if (tempCoc.Length = ) then
begin
for q := to tempCoc.Length - do
begin
emt2 := tempCoc.item(q, ) as IHTMLEleMent;
if emt2.tagName = 'STRONG' then
s2 := emt2.innerText
else if emt2.tagName = 'A' then
begin
s2 := emt2.getAttribute('href',);
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;

elmt: IHTMLEleMent;

elmt := (WebBrowser1.Document as ihtmldocument3).getElementById('idbtn001');

getElementsByName

getElementById

getElementsByTagName

doc2: IHTMLDocument2;
doc3: IHTMLDocument3;

doc2.forms.item('form1',0) as IHTMLFormElement;//取form1的表单

elmt := (WebBrowser1.Document as IHTMLDocument3).getElementById('divfirstID');//通过ID取得某个节点

elmt := ((elmt.children as IHTMLElementCollection).item(, )) as IHTMLEleMent;//子节点
elmt := ((elmt.children as IHTMLElementCollection).item(, )) as IHTMLEleMent;//子节点
elmt := ((elmt.children as IHTMLElementCollection).item(, )) as IHTMLEleMent;//子节点
elmt := ((elmt.children as IHTMLElementCollection).item(, )) as IHTMLEleMent;//子节点里的第3个子节点
elmt := ((elmt.children as IHTMLElementCollection).item(, )) as IHTMLEleMent;//子节点

Delphi 解析HTML的更多相关文章

  1. DELPHI解析JSON格式化的日期

    DELPHI解析JSON格式化的日期 json返回的日期是 /Date(1560355200000)/ 这样的格式. 这个1560355200000,是指1970年以后的秒数. DELPHI如何解析这 ...

  2. delphi 解析Json格式(转自:http://blog.csdn.net/jayqiang/article/details/7066824)

    SuperObject 是开源的 Delphi 的 JSON 工具包,可生成 JSON 数据以及进行 JSON 解析. unit Unit6; interface uses Windows, Mess ...

  3. Delphi解析修改Json文件,基于superobject.pas(ISuperObject)

    在经过一系列的波折后,还是觉得delphi读取并修改json文件来的方便: 在网络上找到一个delphi的三方库ISuperObject,添加到项目后直接引用就行: 下载地址 ISuperObject ...

  4. 让delphi解析chrome扩展的native应用

    chrome浏览器自从去年以来逐步去掉了对浏览器插件的支持,npapi的方案马上不可用. 当务之急要选择一个替代方案,最常用的就是扩展了.扩展程序提供了一套和本地程序交互的方案——“原生消息通信” 写 ...

  5. Delphi 解析系统环境变量

    // http://www.outofmemory.cn function ExpandEnvironment(const strValue: string): string; var chrResu ...

  6. Delphi经验总结(1)

    先人的DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername ...

  7. [转]Delphi 控件属性和事件

    常用[属性] Action:该属性是与组件关联的行为,允许应用程序集中响应用户命令 Anchors:与组件连接的窗体的位置点 Align:确定组件的对齐方式 AutoSize:确定组件是否自动调整其大 ...

  8. Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示

    Delphi 读取 c# webservice XML的base64编码图片字符串转化图片并显示 在 开发中遇到应用c#及asp.net的在的webservice 保存图片并以xml文件形式现实出来 ...

  9. copy other

    DELPHI基础开发技巧 ◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername/di ...

随机推荐

  1. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

  2. MySQL Disk--磁盘相关参数

    /sys/block/sda/queue/nr_requests 磁盘队列长度.默认只有 128 个队列,可以提高到 512 个.会更加占用内存,但能更加多的合并读写操作,速度变慢,但能读写更加多的量 ...

  3. 【转】每天一个linux命令(53):route命令

    原文网址:http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html Linux系统的route命令用于显示和操作IP路由表(show / ...

  4. 【转】每天一个linux命令(48):watch命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/12/31/2840241.html watch是一个非常实用的命令,基本所有的Linux发行版都带有这个 ...

  5. R(8): tidyr

    tidy(整洁),Tidyr包是由Hadely Wickham创建,这个包提高了整理原始数据的效率,tidyr包的4个常用的函数及其用途如下: gather()——它把多列放在一起,然后转化为key: ...

  6. 解决eclipse中断点调试不起作用的问题

    解决eclipse中断点调试不起作用的问题   eclipsegeneration编译器file工作 最近几天,遇到了一个问题,就是在eclipse中进行断点调试程序到时候,跟踪不到我设置的断点.困惑 ...

  7. Extjs 分页传参方法

    第一种(常用): var proxy = new Ext.data.HttpProxy({url : url}) var store = new Ext.data.Store({ pruneModif ...

  8. string截断

    public static string GetFirstString(string stringToSub, int length)         {            Regex regex ...

  9. ThinkJava-容器深入研究

    第17章 容器深入研究 17.2 填充容器 package com.cy.container; import java.util.*; class StringAddress { private St ...

  10. 每秒查询率QPS

    每秒查询率QPS是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准,在因特网上,作为域名系统服务器的机器的性能经常用每秒查询率来衡量. 原理:每天80%的访问集中在20%的时间里,这20%时 ...