this slowpoke moves

Parse Text from HTML Document

Das folgende Beispiel zeigt, wie man den gesamten Text eines HTML-Dokuments extrahiert und in ein Memo ladet.

Zunächst muss die "MSHTML_TLB.pas" Datei eingebunden werden, die ihr hier herunterladen könnt:
uses ExtCtrls, MSHTML_TLB, ActiveX, ComObj

//

procedure TForm1.Button1Click(Sender: TObject);
var
  IDoc: IHTMLDocument2;
  Strl: TStringList;
  sHTMLFile: string;
  v: Variant;
  Links: IHTMLElementCollection;
  i: Integer;
  Link: IHTMLAnchorElement;
begin
  if OpenDialog1.Execute then
  begin
    sHTMLFile := OpenDialog1.FileName;
    Strl := TStringList.Create;
    try
      Strl.LoadFromFile(sHTMLFile);
      Idoc := CreateComObject(Class_HTMLDOcument) as IHTMLDocument2;
      try
        IDoc.designMode := 'on';
        while IDoc.readyState <> 'complete' do
          Application.ProcessMessages;
        v := VarArrayCreate([0, 0], VarVariant);
        v[0] := Strl.Text;
        IDoc.Write(PSafeArray(System.TVarData(v).VArray));
        IDoc.designMode := 'off';
        while IDoc.readyState <> 'complete' do
          Application.ProcessMessages;
        Memo1.Lines.Text := IDoc.body.innerText;
      finally
        IDoc := nil;
      end;
    finally
      Strl.Free;
    end;
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate