How do I create an IHTMLDocument2 using a string from TIdHTTP?

二次信任 提交于 2019-11-27 07:06:43

问题


I download a URL with IdHTTP.Get, and I need to search the HTML tags and extract some data.

How I can convert the string that IdHTTP.Get returns into an IHTMLDocument2?


回答1:


Try this one:

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;



回答2:


I Googled this problem and I can find a good code for this:

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;

Regards



来源:https://stackoverflow.com/questions/11915903/how-do-i-create-an-ihtmldocument2-using-a-string-from-tidhttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!