Solved

HTML parser needed to generate URL/text pairs

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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