我是在做一个外部程序。 我已找到一段用VB取得网页中文本的代码,我想很容易用delphi重写出来,但我不知道如何得到所有的网页中的对象,如图片等。 代码如下: Dim objDoc As Object Dim objIE As Object For Each objIE In dWinFolder If objIE.LocationURL = List1.List(List1.ListIndex) Then Set objDoc = objIE.Document For i = 1 To objDoc.All.length - 1 If objDoc.All(i).tagname = "BODY" Then Text1.Text = objDoc.All(i).innerText End If Next Exit For End If Next还望高手赐教。
to TechnoFantasy(www.applevb.com) : 我看了你提供的帖子中的代码,如下: procedure TForm1.Button1Click(Sender: TObject); var FDoc:IHTMLDocument2; begin FDoc:=Webbrowser1.Document as IHTMLDocument2; if assigned(FDoc)then memo1.text:=FDoc.body.innerHTML; end;procedure TForm1.Button2Click(Sender: TObject); begin webbrowser1.Navigate('http://www.applevb.com'); end;上面的代码只得到网页中的文本,可是我要得到网页中的所有内容,并存到硬盘上,怎么办呢? 还有网友知道解决办法吗?
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls, SHDocVw, activeX;type TForm1 = class(TForm) WebBrowser1: TWebBrowser; Button1: TButton; SaveDialog1: TSaveDialog; Button2: TButton; Button3: TButton; procedure SaveHTMLSourceToFile(const FileName: string;WB: TWebBrowser); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure SimulateKeyDown(Key : byte); begin keybd_event(Key, 0, 0, 0); end;procedure SimulateKeyUp(Key : byte); begin keybd_event(Key, 0, KEYEVENTF_KEYUP, 0); end;procedure SimulateKeystroke(Key : byte; extra : DWORD); begin keybd_event(Key, extra, 0, 0); keybd_event(Key, extra, KEYEVENTF_KEYUP, 0); end;procedure SendKeys(s : string); var i : integer; flag : bool; w : word; begin {Get the state of the caps lock key} flag := not GetKeyState(VK_CAPITAL) and 1 = 0; {If the caps lock key is on then turn it off} if flag then SimulateKeystroke(VK_CAPITAL, 0); for i := 1 to Length(s) do begin w := VkKeyScan(s[i]); {If there is not an error in the key translation} if ((HiByte(w) <> $FF) and (LoByte(w) <> $FF)) then begin {If the key requires the shift key down - hold it down} if HiByte(w) and 1 = 1 then SimulateKeyDown(VK_SHIFT); {Send the VK_KEY} SimulateKeystroke(LoByte(w), 0); {If the key required the shift key down - release it} if HiByte(w) and 1 = 1 then SimulateKeyUp(VK_SHIFT); end; end; {if the caps lock key was on at start, turn it back on} if flag then SimulateKeystroke(VK_CAPITAL, 0); end;procedure TForm1.SaveHTMLSourceToFile(const FileName: string; WB: TWebBrowser); var PersistStream: IPersistStreamInit; FileStream: TFileStream; Stream: IStream; SaveResult: HRESULT; begin PersistStream := WB.Document as IPersistStreamInit; FileStream := TFileStream.Create(FileName, fmCreate); try Stream := TStreamAdapter.Create(FileStream, soReference) as IStream; SaveResult := PersistStream.Save(Stream, True); if FAILED(SaveResult) then MessageBox(Handle, 'Fail to save HTML source', 'Error', 0); finally { we are passing soReference in TStreamAdapter constructor, it is our responsibility to destroy the TFileStream object. } FileStream.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if SaveDialog1.Execute then SaveHTMLSourceToFile(SaveDialog1.FileName, WebBrowser1); end; procedure TForm1.Button2Click(Sender: TObject); begin webbrowser1.Navigate('www.csdn.net'); end;procedure TForm1.Button3Click(Sender: TObject); var Vin,Vout : OleVariant; savepath, keysequence: string; begin vin:='aaaaa.mht'; vout:=''; // WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,vin, vout); // WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DONTPROMPTUSER,vin, vout); savepath:='c:\tmp\'; keysequence:=Format('%s%s',[savepath, FormatDateTime('yymmddmmss',now)+'.mht']); sendkeys(keysequence); SimulateKeyStroke(VK_TAB,0); SimulateKeyStroke(VK_DOWN,0); SimulateKeyStroke(VK_DOWN,0); simulateKeyStroke(VK_RETURN,0); SimulateKeyStroke(VK_RETURN,0); WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_PROMPTUSER); end;end.
To ChinaDelphiFan(): 1。你的方法我已试过,仍是调用了IE的execWB,这是一种解决方案。采用这种方案应该会出现文件保存对话框,然后是一闪而过。再保存。 2。可我看到一个软件Elib,据我的研究,它没有用这种方案,而是用了与IHTMLDocument2有关的其它方法。所以保存网页上的所有内容时,根本没有出现SaveAs窗口。 不知作者是用了什么好方法,我关心的是这个。不过仍谢谢你,找到更好的方法后我会给分的。 Elib的网址是www.shijun.com
其次是获得Document的文档文本,方法如下:
http://www.csdn.net/expert/topic/294/294762.shtm
我已找到一段用VB取得网页中文本的代码,我想很容易用delphi重写出来,但我不知道如何得到所有的网页中的对象,如图片等。
代码如下:
Dim objDoc As Object
Dim objIE As Object
For Each objIE In dWinFolder
If objIE.LocationURL = List1.List(List1.ListIndex) Then
Set objDoc = objIE.Document
For i = 1 To objDoc.All.length - 1
If objDoc.All(i).tagname = "BODY" Then
Text1.Text = objDoc.All(i).innerText
End If
Next
Exit For
End If
Next还望高手赐教。
我看了你提供的帖子中的代码,如下:
procedure TForm1.Button1Click(Sender: TObject);
var
FDoc:IHTMLDocument2;
begin
FDoc:=Webbrowser1.Document as IHTMLDocument2;
if assigned(FDoc)then
memo1.text:=FDoc.body.innerHTML;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
webbrowser1.Navigate('http://www.applevb.com');
end;上面的代码只得到网页中的文本,可是我要得到网页中的所有内容,并存到硬盘上,怎么办呢?
还有网友知道解决办法吗?
弹出保存提示框。另外一个方法就是自己写代码分析获取的页面源文件,将其中的连接在
下载保存。
第二种方法不好,因为我不想再一次下载.
我其实是在做一个保存IE网页的软件.我看到很多类似软件可以做到不用ExecWB,而可以保存网页及其中的所有内容.我现在已能Document的innerHTML方法来得到网页源代码,却不知如何得到网页中的图片,并将其保存成文件.
哪位能再指点一下?
1、根据给出的URL,下在页面(假设叫A.HTML)到本地。
2、解析出A.HTML中的感兴趣的对象:如图象,插件,声音等的URL(解析可利用DOM)
3、根据上述对象的URL,使用函数URLDownloadToFile可以把他们保存在文件中,该函数
在单元URLMON.PAS中,而且还有好多其他的URL方面的函数你可以使用。
4、必要的情况下,要修改A.HTML页面中的某些连接,使其成为本地连接。
你建议用urlDownloadToFile函数,似仍不能达到我的要求,该函数好象仍要根据得到的url再下载一次,而不是从内存中将网页中的对象保存成文件.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw, activeX;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
SaveDialog1: TSaveDialog;
Button2: TButton;
Button3: TButton; procedure SaveHTMLSourceToFile(const FileName: string;WB: TWebBrowser);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure SimulateKeyDown(Key : byte);
begin
keybd_event(Key, 0, 0, 0);
end;procedure SimulateKeyUp(Key : byte);
begin
keybd_event(Key, 0, KEYEVENTF_KEYUP, 0);
end;procedure SimulateKeystroke(Key : byte;
extra : DWORD);
begin
keybd_event(Key,
extra,
0,
0);
keybd_event(Key,
extra,
KEYEVENTF_KEYUP,
0);
end;procedure SendKeys(s : string);
var
i : integer;
flag : bool;
w : word;
begin
{Get the state of the caps lock key}
flag := not GetKeyState(VK_CAPITAL) and 1 = 0;
{If the caps lock key is on then turn it off}
if flag then
SimulateKeystroke(VK_CAPITAL, 0);
for i := 1 to Length(s) do begin
w := VkKeyScan(s[i]);
{If there is not an error in the key translation}
if ((HiByte(w) <> $FF) and
(LoByte(w) <> $FF)) then begin
{If the key requires the shift key down - hold it down}
if HiByte(w) and 1 = 1 then
SimulateKeyDown(VK_SHIFT);
{Send the VK_KEY}
SimulateKeystroke(LoByte(w), 0);
{If the key required the shift key down - release it}
if HiByte(w) and 1 = 1 then
SimulateKeyUp(VK_SHIFT);
end;
end;
{if the caps lock key was on at start, turn it back on}
if flag then
SimulateKeystroke(VK_CAPITAL, 0);
end;procedure TForm1.SaveHTMLSourceToFile(const FileName: string;
WB: TWebBrowser);
var
PersistStream: IPersistStreamInit;
FileStream: TFileStream;
Stream: IStream;
SaveResult: HRESULT;
begin
PersistStream := WB.Document as IPersistStreamInit;
FileStream := TFileStream.Create(FileName, fmCreate);
try
Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
SaveResult := PersistStream.Save(Stream, True);
if FAILED(SaveResult) then
MessageBox(Handle, 'Fail to save HTML source', 'Error', 0);
finally
{ we are passing soReference in TStreamAdapter constructor,
it is our responsibility to destroy the TFileStream object. }
FileStream.Free;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
SaveHTMLSourceToFile(SaveDialog1.FileName, WebBrowser1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
webbrowser1.Navigate('www.csdn.net');
end;procedure TForm1.Button3Click(Sender: TObject);
var
Vin,Vout : OleVariant;
savepath, keysequence: string;
begin
vin:='aaaaa.mht';
vout:='';
// WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,vin, vout);
// WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DONTPROMPTUSER,vin, vout); savepath:='c:\tmp\';
keysequence:=Format('%s%s',[savepath, FormatDateTime('yymmddmmss',now)+'.mht']);
sendkeys(keysequence);
SimulateKeyStroke(VK_TAB,0);
SimulateKeyStroke(VK_DOWN,0);
SimulateKeyStroke(VK_DOWN,0);
simulateKeyStroke(VK_RETURN,0);
SimulateKeyStroke(VK_RETURN,0);
WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_PROMPTUSER);
end;end.
1。你的方法我已试过,仍是调用了IE的execWB,这是一种解决方案。采用这种方案应该会出现文件保存对话框,然后是一闪而过。再保存。
2。可我看到一个软件Elib,据我的研究,它没有用这种方案,而是用了与IHTMLDocument2有关的其它方法。所以保存网页上的所有内容时,根本没有出现SaveAs窗口。
不知作者是用了什么好方法,我关心的是这个。不过仍谢谢你,找到更好的方法后我会给分的。
Elib的网址是www.shijun.com
有问题可以切磋。
关于导出缓存中的内容,我前几天也想到了。
1。我看了缓存目录,和普通目录不同,多了一项internet地址,这里的内容是从哪里得来?
2。能告诉我从其中导出文件的具体方法吗?
给分后,我又发了一个相关贴子,请看:
http://www.csdn.net/expert/TopicView.asp?id=302193