procedure TForm1.Button1Click(Sender: TObject); var i: Integer; HrefItem: OleVariant; begin Webbrowser1.Navigate('http://www.yahoo.com'); while WebBrowser1.ReadyState < READYSTATE_INTERACTIVE do //頁面激活 Application.ProcessMessages; if WebBrowser1.OleObject.Document.all.tags('A').Length = 0 then Exit; Memo1.Clear; for i := 0 to WebBrowser1.OleObject.Document.all.tags('A').Length - 1 do begin HrefItem := WebBrowser1.OleObject.Document.all.tags('A').Item(i); Memo1.Lines.Add( HrefItem ); end; end;(注:用到TWebBrowser控件。 取得<a href="..." >的内容、不知是不是NI的要求?)
和cronuz(cronus) 类似,wb为TWebBrowser procedure TfrmWebInfo.FormCreate(Sender: TObject); begin wb.Navigate('http://www.sohu.com') end;procedure TfrmWebInfo.btnGetHrefClick(Sender: TObject); var ov: OleVariant; i: Integer; begin if wb.Busy then Exit; {未加载完判断} meoInfo.Lines.Clear; ov := wb.OleObject.Document.all; for i := 0 to (ov.Length - 1) do if ov.Item(i).tagName = 'A' then meoInfo.Lines.Append(ov.Item(i).href+'1'+ov.Item(i).InnerText) end;
嘿嘿,给一个Borland官方的解决方法给你了 绝对正确!!!!!可以在 http://codecentral.borland.com下载的 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.
var
i: Integer;
HrefItem: OleVariant;
begin
Webbrowser1.Navigate('http://www.yahoo.com');
while WebBrowser1.ReadyState < READYSTATE_INTERACTIVE do //頁面激活
Application.ProcessMessages; if WebBrowser1.OleObject.Document.all.tags('A').Length = 0 then Exit;
Memo1.Clear; for i := 0 to WebBrowser1.OleObject.Document.all.tags('A').Length - 1 do
begin
HrefItem := WebBrowser1.OleObject.Document.all.tags('A').Item(i);
Memo1.Lines.Add( HrefItem );
end;
end;(注:用到TWebBrowser控件。
取得<a href="..." >的内容、不知是不是NI的要求?)
procedure TfrmWebInfo.FormCreate(Sender: TObject);
begin
wb.Navigate('http://www.sohu.com')
end;procedure TfrmWebInfo.btnGetHrefClick(Sender: TObject);
var
ov: OleVariant;
i: Integer;
begin
if wb.Busy then Exit; {未加载完判断}
meoInfo.Lines.Clear;
ov := wb.OleObject.Document.all;
for i := 0 to (ov.Length - 1) do
if ov.Item(i).tagName = 'A' then
meoInfo.Lines.Append(ov.Item(i).href+'1'+ov.Item(i).InnerText)
end;
绝对正确!!!!!可以在 http://codecentral.borland.com下载的
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.