Solved

HTML parser needed to generate URL/text pairs

Posted on 2003-11-10
2
471 Views
Last Modified: 2010-04-05
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
Comment
Question by:Monroe406
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Assisted Solution

by:mohammedasd
mohammedasd earned 25 total points
ID: 9721962
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
 
LVL 26

Accepted Solution

by:
EddieShipman earned 25 total points
ID: 9725981
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question