[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

HTML parser needed to generate URL/text pairs

I need a way to collect in pairs, the associated text with each hyperlink/URL on a HTML page.  Example:
 
<html>
<header></header>
<body>
This <a href="http:\\www.legitima.com\testing.htm">is a test</a> of what I mean.
</body>
</html>
 
I need a HTML parser that will populate a string list or string array with the "is a test" text and the associated "http:\\www.legitima.com\testing.htm" URL.
 
If this is still not clear, for example's sake... imagine a 2 field database, with fields "Text" and "URL".   For each hyperlink on a HTML page, I need to have one record created, containg the appropriate "text" and "URL" fields, for each hyperlink.

I've looked at Torry's for parser components, but the two most popular ones are shareware products, and their respective owners are dead or missing in action.  With respect to TLegHTML, email sent to legitima.com bounces back to sender, and with DIHtmlParser the author's web site is dead.

Any suggestions on components that are still alive and that perform the above task?
0
Monroe406
Asked:
Monroe406
2 Solutions
 
mohammedasdCommented:
try to use the following Procedure which
- str is  HTML code string:

procedure Tform1.paraseHTML(str:string);
var i:integer;
   strURL,  //content the value of URl
   Strtext //content the value of Text of the above URL
   :string;
begin
  while pos('href="',str)<>0 do
   begin
     delete(str,1,pos('href="',str)+4);
     strUrl:=copy(str,2,pos('">',str)-2);
     delete(str,1,pos('">',str)+1);
     strText:=copy(str,1,pos('</a>',str)-1);
     // the following two lines to add the URl &Text to memo
     //instead you can add them to table in database;
       memo2.Lines.Add(strurl);
       memo2.Lines.Add(strtext);
   end;
end;
0
 
Eddie ShipmanAll-around developerCommented:
In this example, I used a component called extIEParser that you can get
from the delphi-webbrowser yahoo group files section.

http://groups.yahoo.com/group/delphi-webbrowser

(you must be registered on the groups to get the file. It's free and
the groups contains LOTS of useful info for you.)

If you'd rather not register, let me know and I'll send it to you via email.

type
  TForm1 = class(TForm)
    Button1: TButton;
    extIEParser1: TextIEParser;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject); // create the stringlists here
    procedure FormDestroy(Sender: TObject); // free the stringlists here
    procedure extIEParser1Anchor(Sender: TObject; href, target, rel, rev,
      urn, Methods, name, host, hostname, pathname, port, protocol, search,
      hash, accesskey, protocolLong, mimeType, nameProp: String;
      Element: TElementInfo);
  private
    { Private declarations }
  public
    { Public declarations }
    slAnchors, slAnchorHrefs: TStringList;
  end;

  .
  .
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  sURL: String;
begin
  memo1.Clear;
  sURL := 'http://localhost/testing.html'; // I copied the html from your
  // example and it returned
  // in Memo1: is a test
  // in Memo2: http://www.legitima.com/testing.htm
  //
  // Assign the main URL to the extIEParser and Execute.
  extIEParser1.Url := sURL;
  extIEParser1.Execute;
  // slAnchors, and slAnchorHrefs are loaded in extIEParser1Anchor.
  Memo1.Lines.Assign(slAnchors);
  Memo2.Lines.Assign(slAnchorHrefs);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  slAnchorHrefs := TStringList.Create;
  slAnchors       := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  slAnchorHrefs.Free;
  slAnchors.Free;
end;

procedure TForm1.extIEParser1Anchor(Sender: TObject; href, target, rel,
  rev, urn, Methods, name, host, hostname, pathname, port, protocol,
  search, hash, accesskey, protocolLong, mimeType, nameProp: String;
  Element: TElementInfo);
begin
  slAnchorHrefs.Add(href);
  if Trim(Element.outerText) = '' then
    slAnchors.Add('unassgned')
  else
  slAnchors.Add(Element.outerText);
end;

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now