6

我用 下载了一个 URL IdHTTP.Get,我需要搜索 HTML 标签并提取一些数据。

如何将IdHTTP.Get返回的字符串转换为IHTMLDocument2?

4

2 回答 2

6

试试这个:

uses
  ... Variants, MSHTML, ActiveX;

var Cache: string;
    V: OleVariant;
    Doc: IHTMLDocument2;
begin
  ...

  Cache := IdHTTP.Get(url);
  Doc := coHTMLDocument.Create as IHTMLDocument2; // create IHTMLDocument2 instance
  V := VarArrayCreate([0,0], varVariant);
  V[0] := Cache;
  Doc.Write(PSafeArray(TVarData(v).VArray)); // write data from IdHTTP

  // Work with Doc
end;
于 2012-08-12T08:31:15.977 回答
3

我用谷歌搜索了这个问题,我可以找到一个很好的代码:

Idoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;
try
  IDoc.designMode := 'on';
  while IDoc.readyState <> 'complete' do
    Application.ProcessMessages;
  v := VarArrayCreate([0, 0], VarVariant);
  v[0] := MyHTML;
  IDoc.Write(PSafeArray(System.TVarData(v).VArray));
  IDoc.designMode := 'off';
  while IDoc.readyState <> 'complete' do
    Application.ProcessMessages;

  ParseHTML(IDoc);
finally
  IDoc := nil;
end;

问候

于 2012-08-11T15:57:59.300 回答