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. ES6必知必会 (四)—— Symbol、Set和Map

    Symbol 1.Symbol 是 ES6 引入了一种新的原始数据类型,表示独一无二的值.它是 JavaScript 语言的第七种数据类型,前六种分别是:undefined.null.布尔值(Bool ...

  2. vault 集群搭建(active standby 模式)

        参考架构图: consul server cluster 搭建 consul 基本配置格式 { "server": true, "node_name": ...

  3. stenciljs 学习十一 pwa 支持

    stenciljs 对于pwa 的支持是自动注入的,我们只需要简单的配置,stenciljs使用workbox 配置 默认配置 { skipWaiting: true, clientsClaim: t ...

  4. 模板引擎jade学习

    语言参考 标签列表 doctype Tags Block Expansion Attributes Boolean Attributes Class Attributes Class Literal ...

  5. debian的pdf阅读器

    www.foxitsoftware.cn下载deb包 或者 sudo apt-get install evince

  6. js jquery 设置cookie

    转自http://yaoqianglilan.blog.163.com/blog/static/70978316201091810435251/ 本人亲测setcookie() getcookie() ...

  7. session 、cookie、token的区别

    session session的中文翻译是“会话”,当用户打开某个web应用时,便与web服务器产生一次session.服务器使用session把用户的信息临时保存在了服务器上,用户离开网站后sess ...

  8. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

  9. sqoop产生背景及概述

    sqoop产生背景 多数是用Hadoop技术处理大数据业务的企业有大量的数据存储在传统的关系型数据库(RDBMS)中:由于缺乏工具的支持.对Hadoop和传统数据库系统中的数据进行相互传输是一件十分困 ...

  10. PHP mysqli 增强 批量执行sql 语句的实现代码

    本篇文章介绍了,在PHP中 mysqli 增强 批量执行sql 语句的实现代码.需要的朋友参考下. mysqli 增强-批量执行sql 语句 <?php //mysqli 增强-批量执行sql ...