有个比较笨的办法,通过把当前网页的所有链接都取出来,然后一个一个去打开,同时建立目录,抓链接的代码我正好写了一个个,供楼主参考
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, ComCtrls, mshtml;
type
TForm1 = class(TForm)
Button1: TButton;
WebBrowser1: TWebBrowser;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch;
var URL: OleVariant);
private
FFinished: Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
doc: IHTMLDocument2;
all: IHTMLElementCollection;
len, i: integer;
item: OleVariant;
listitem: TListItem;
begin
if not FFinished then
ShowMessage('网页正在下载中……')
else begin
doc := WebBrowser1.Document as IHTMLDocument2;
all := doc.Get_links; //doc.Links亦可
len := all.length;
for i := 0 to len - 1 do begin
item := all.item(i, varempty);
listitem := ListView1.Items.Add;
listitem.Caption := item.href;
listitem.SubItems.Add(item.innertext);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.163.com');
FFinished := False;
end;
procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
FFinished := True;
end;
end.
下一步就是在上面的Button1Click修改,循环取所有链接,知道没有链接为止