We help IT Professionals succeed at work.

Extract from file...

Vincentg
Vincentg asked
on
Medium Priority
149 Views
Last Modified: 2010-04-06
Hello,

I need to extract the title from an HTML document on my local drive.
How can i read the html code and put everything between <title> and </title> in a string.?

Thanks,

Vincent
Comment
Watch Question

Commented:
function GetTitle(AFileName: string): string;
var
  F: TextFile;
  Line: string;
begin
  Result:= '';
  AssignFile(F, AFileName);
  Reset(F);
  while not Eof(F) do
  begin
    Readln(F, Line);

    // Get question title
    if (Pos('<TITLE>', UpperCase(Line)) > 0) then
    begin
      Result:= Copy(Line, Pos('<TITLE>', UpperCase(Line)) + 7, Length(Line));
      Result:= Copy(Result, 1, Pos('-', Result) - 1);
      Break;
    end; // if (ATitle..

  end; // while
  CloseFile(F);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:= GetTitle('d:\Internet\experts\Delphi, modem in delphi.htm');
end;


Motaz

Commented:
Another solution:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    w: TWebBrowser;
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure wDocumentComplete(Sender: TObject; const pDisp: IDispatch;
      var URL: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
        w.Navigate(edit2.Text);        
end;

procedure TForm1.wDocumentComplete(Sender: TObject; const pDisp: IDispatch;
  var URL: OleVariant);
var
        doc:variant;
begin
        doc:=w.Document;
        edit1.Text:=doc.title;
end;

end.

Author

Commented:
Thanks a lot, it works for me :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.