Mega Code Archive

 
Categories / Delphi / Examples
 

The easy way of getting links

Title: The easy way of getting links Question: How do I get the links from a HTML document? Answer: Heres the easyest way to get all the links in a HTML document I found. How? Using the WebBrowser component from the Internet pallete of Delphi. --- { All we need here is a TWebBrowser component named WebBrowser1, a button named Button1 to navigate and a Button2 to get the links } unit main; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, SHDocVw, mshtml; type TForm1 = class(TForm) WebBrowser1: TWebBrowser; Button1: TButton; Button2: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var urlnavigate : OleVariant; begin urlnavigate := Edit1.text; WebBrowser1.Navigate2(urlnavigate); end; procedure TForm1.Button2Click(Sender: TObject); var Document : IHtmlDocument2; Link : IHTMLElement; StrLinks : string; i : integer; begin Document := WebBrowser1.Document as IHTMlDocument2; for i := 0 to Document.Links.Length - 1 do begin Link := Document.Links.Item(i, 0) as IHTMLElement; StrLinks := Link.ToString + #13#10; ShowMessage(StrLinks); end; end; end. --- If you want to make a robot, for example, you can use the WebBrowsers events to get the links when the page is loaded, instead of cliking a button. Have fun!