Solved

HTML parser needed to generate URL/text pairs

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

763 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