通过IE的内置解释系统都可以做到一样的效果,OLE操作的,网上这类型的实例最多的自己解释就真的很麻烦的了ThttpScan我以前用过,没有代码的组件,一点都不好用的unit MainFrm;{ Automating IE - Finding Links by Corbin Dunn, [email protected] Delphi Developer Support This code is on: http://codecentral.borland.com And the document describing it can be found at: http://community.borland.com or http://search.borland.com }interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, OleServer, StdCtrls, OleCtrls, SHDocVw;type TForm1 = class(TForm) btnFindLinks: TButton; edtURL: TEdit; lblURL: TLabel; lstbxLinks: TListBox; procedure FormCreate(Sender: TObject); procedure btnFindLinksClick(Sender: TObject); private FInternetExplorer: TInternetExplorer; procedure WebBrowserDocumentComplete(Sender: TObject; var pDisp: OleVariant; var URL: OleVariant); public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM} uses MSHTML_TLB, ComObj;procedure TForm1.FormCreate(Sender: TObject); begin FInternetExplorer := TInternetExplorer.Create(Self); FInternetExplorer.OnDocumentComplete := WebBrowserDocumentComplete; end;procedure TForm1.WebBrowserDocumentComplete(Sender: TObject; var pDisp: OleVariant; var URL: OleVariant); var Doc: IHTMLDocument2; ElementCollection: IHTMLElementCollection; HtmlElement: IHTMLElement; I: Integer; AnchorString: string; begin lstbxLinks.Clear; // We will process the document at this time. Trying to do // so earlier won't work because it hasn't fully loaded. Doc := FInternetExplorer.Document as IHTMLDocument2; if Doc = nil then raise Exception.Create('Couldn''t convert the ' + 'FInternetExplorer.Document to an IHTMLDocument2'); // First, grab all the elements on the web page ElementCollection := Doc.all; for I := 0 to ElementCollection.length - 1 do begin // Get the current element HtmlElement := ElementCollection.item(I, '') as IHTMLElement; // Next, check to see if it is a link (tagName will be A). // You could easily find other tags (such as TABLE, FONT, etc.) if HTMLElement.tagName = 'A' then begin // Now grab the innerText for this particular link. The innerText is // all text that is inside of the particular tag. For example, // this would give us "Go To Borland" from the HTML: // <a href="http://www.borland.com"><b>Go To Borland</b></a>. // If you want "<b>Go To Borland</b>" use innerHTML. AnchorString := HtmlElement.innerText; if AnchorString = '' then AnchorString := '(Empty Name)'; // We know that the element is an IHTMLAnchorElement since the tagName // is 'A'. AnchorString := AnchorString + ' - ' + (HtmlElement as IHTMLAnchorElement).href; lstbxLinks.Items.Add(AnchorString); end; end; end;procedure TForm1.btnFindLinksClick(Sender: TObject); begin // Simply browse to the given page FInternetExplorer.Navigate(edtURL.Text, EmptyParam, EmptyParam, EmptyParam, EmptyParam); end;end.
Automating IE - Finding Links
by Corbin Dunn, [email protected]
Delphi Developer Support
This code is on: http://codecentral.borland.com
And the document describing it can be found at:
http://community.borland.com or
http://search.borland.com
}interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
OleServer, StdCtrls, OleCtrls, SHDocVw;type
TForm1 = class(TForm)
btnFindLinks: TButton;
edtURL: TEdit;
lblURL: TLabel;
lstbxLinks: TListBox;
procedure FormCreate(Sender: TObject);
procedure btnFindLinksClick(Sender: TObject);
private
FInternetExplorer: TInternetExplorer;
procedure WebBrowserDocumentComplete(Sender: TObject; var pDisp: OleVariant;
var URL: OleVariant);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}
uses MSHTML_TLB, ComObj;procedure TForm1.FormCreate(Sender: TObject);
begin
FInternetExplorer := TInternetExplorer.Create(Self);
FInternetExplorer.OnDocumentComplete := WebBrowserDocumentComplete;
end;procedure TForm1.WebBrowserDocumentComplete(Sender: TObject;
var pDisp: OleVariant; var URL: OleVariant);
var
Doc: IHTMLDocument2;
ElementCollection: IHTMLElementCollection;
HtmlElement: IHTMLElement;
I: Integer;
AnchorString: string;
begin
lstbxLinks.Clear;
// We will process the document at this time. Trying to do
// so earlier won't work because it hasn't fully loaded.
Doc := FInternetExplorer.Document as IHTMLDocument2;
if Doc = nil then
raise Exception.Create('Couldn''t convert the ' +
'FInternetExplorer.Document to an IHTMLDocument2');
// First, grab all the elements on the web page
ElementCollection := Doc.all;
for I := 0 to ElementCollection.length - 1 do
begin
// Get the current element
HtmlElement := ElementCollection.item(I, '') as IHTMLElement;
// Next, check to see if it is a link (tagName will be A).
// You could easily find other tags (such as TABLE, FONT, etc.)
if HTMLElement.tagName = 'A' then
begin
// Now grab the innerText for this particular link. The innerText is
// all text that is inside of the particular tag. For example,
// this would give us "Go To Borland" from the HTML:
// <a href="http://www.borland.com"><b>Go To Borland</b></a>.
// If you want "<b>Go To Borland</b>" use innerHTML.
AnchorString := HtmlElement.innerText;
if AnchorString = '' then
AnchorString := '(Empty Name)';
// We know that the element is an IHTMLAnchorElement since the tagName
// is 'A'.
AnchorString := AnchorString + ' - ' +
(HtmlElement as IHTMLAnchorElement).href;
lstbxLinks.Items.Add(AnchorString);
end;
end;
end;procedure TForm1.btnFindLinksClick(Sender: TObject);
begin
// Simply browse to the given page
FInternetExplorer.Navigate(edtURL.Text, EmptyParam, EmptyParam,
EmptyParam, EmptyParam);
end;end.