Solved

HTML parser needed to generate URL/text pairs

Posted on 2003-11-10
2
460 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
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now