博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi 解析HTML
阅读量:4324 次
发布时间:2019-06-06

本文共 3194 字,大约阅读时间需要 10 分钟。

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'', '', [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组件开发及应用

 

 

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

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;

 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(0, 0)) as IHTMLEleMent;//子节点elmt := ((elmt.children as IHTMLElementCollection).item(0, 0)) as IHTMLEleMent;//子节点elmt := ((elmt.children as IHTMLElementCollection).item(0, 0)) as IHTMLEleMent;//子节点elmt := ((elmt.children as IHTMLElementCollection).item(2, 0)) as IHTMLEleMent;//子节点里的第3个子节点elmt := ((elmt.children as IHTMLElementCollection).item(0, 0)) as IHTMLEleMent;//子节点

 

转载于:https://www.cnblogs.com/cb168/p/3918237.html

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_4、快速创建SpringBoot应用之自动创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_8、SpringBoot基础HTTP接口GET请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_6、SpringBoot2.xHTTP请求配置讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_10、常用json框架介绍和Jackson返回结果处理...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_11、SpringBoot2.x目录文件结构讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_15、SpringBoot2.x配置文件讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_13、jar包方式运行web项目文件上传和访问...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_14、SpringBoot2.x使用Dev-tool热部署...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_16、注解配置文件自动映射到属性和实体类实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_18、SpringBoot测试进阶高级篇之MockMvc讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_36、SpringBoot整合mybatis之事务处理实战...
查看>>