webbrowser的HTML问题(100分)

Z

zzhudy

Unregistered / Unconfirmed
GUEST, unregistred user!
我通过IHtmlDocument2(WebBrowser1.Document).Body.OuterHtml这句可以得到
打开网页的HTML,可是怎么判断它有没有包含图片或声音的HTML呢?
例如
<IMG src="images/5175-0.jpg"> 或
http://music.zzti.edu.cn/music/pop/shenhuxi.mp3
 
你是否想过滤掉这些东东?
 
TO WebExplorer:
想保存。当它为图片时保存到一个文件夹中,当为声音时保存到另一个文件夹中
 
怎么就没人回答呢
 
既然已经得到源文件了,分析一下不就都有了吗?关键是要知道HTML TAG
的意义。
 
给你我的程序,思路已在,你要求的也在,不过自己要改改才能用
关于把网页,以及与网页相关的都保存到硬盘中!
这个答案如下:例子的效果把www.sina.com.cn的网页保存下来!:)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,nmhttp,urlmon,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Splitter1: TSplitter;
procedure Button1Click(Sender: TObject);
private
procedure savedata(url,memo:string);
function dealwith(url,baseurl:string):string;
//处理网页路径
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.savedata(url,memo:string);
functiondo
wnloadFile(Source, Dest: string): Boolean;
begin
try
Result:=UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
Result:=False;
end;
end;

function getfilename(s:string):string;
var i:integer;
begin
for i:=length(s)do
wnto 1do
begin
if (s='/')or(s='/') then
break;
end;
result:=copy(s,i+1,length(s)-i);
end;

var i:integer;
imagelist:tstringlist;
bmplist:tstringlist;
tempmemo:string;
begin
pos,endpos:integer;
path:string;
s:string;
memorystream:tmemorystream;
begin

bmplist:=tstringlist.Create ;
bmplist.Add('.bmp');
bmplist.Add('.jpg');
bmplist.Add('.swf');
bmplist.Add('.gif');
path:='d:/download/';
tempmemo:=memo;
imagelist:=tstringlist.create;
try
while pos('src=',memo)>0do
begin
begin
pos:=pos('src=',memo)+4;
while (memo[begin
pos]=' ') or (memo[begin
pos]='"')do
begin
inc(begin
pos);
end;
endpos:=begin
pos+1;
while (memo[endpos]<>' ') and (memo[endpos]<>'"')and (memo[endpos]<>'>')do
begin
inc(endpos);
end;
// s:=copy(memo,begin
pos,endpos-begin
pos);
s:=dealwith(copy(memo,begin
pos,endpos-begin
pos),'www.sina.com.cn');
// showmessage(s);
if (imagelist.indexof(s)=-1)or(imagelist=nil) then
begin
imagelist.Add(s);
do
wnloadFile(s,'d:/download/'+getfilename(s));
end;
delete(memo,1,endpos);
end;
except
showmessage(inttostr(begin
pos)+','+inttostr(endpos));
end;
showmessage(inttostr(imagelist.count));
for i :=0 to imagelist.count-1do
begin
memo1.text:=stringreplace(memo1.text,imagelist.strings,getfilename(imagelist.strings),[rfReplaceAll]);
end;

memorystream:=tmemorystream.Create ;
memorystream.Position :=0;
imagelist.Free;
bmplist.Free ;
memorystream.Free ;
memo1.Lines.SaveToFile('d:/download/index.htm');
showmessage('ok');
end;

procedure TForm1.Button1Click(Sender: TObject);
var s:string;
nmhttp:tnmhttp;
begin
//建立nmhttp
nmhttp:=tnmhttp.Create(nil);
NMHTTP.InputFileMode := FALSE;
NMHTTP.OutputFileMode := FALSE;
// NMHTTP.ReportLevel := Status_Basic;
try
nmhttp.Get('www.sina.com.cn');
s:=nmhttp.body;
except
end;
memo1.text:=s;
savedata('sss',s);
end;

function Tform1.dealwith(url,baseurl:string):string;
//处理网页路径
var i:integer;
begin
pos:integer;
begin
URL:=stringreplace(url,'"','',[rfReplaceAll]);
if url[1]+url[2]+url[3]+url[4]+url[5]+url[6]+url[7]='http://' then
dealwith:=url;
URL:=stringreplace(url,'/','/',[rfReplaceAll]);
if pos(' ',url)<>0 then
begin
url:=copy(url,1, pos(' ',url)-1);
end;

for i:=length(baseurl)do
wnto 1do
begin
if baseurl='/' then
begin
break;
end;
end;

IF I<>1 then
baseurl:=copy(baseurl,1,i)
else
BASEURL:=BASEURL+'/';
if ((pos('.',url)<>0)and(pos('./',url)=0)and(url[1]<>'/')) or (pos('www.',url)<>0) then
dealwith:=url;
if (pos('../',url)=0)AND (POS('./',URL)=0) then
//没有指定目录//??要改改
begin
if url[1]='/' then
url:=copy(url,2,length(url)-1);
dealwith:=baseurl+url;
end
else
begin
i:=length(baseurl)-1;
while pos('../',url)<>0do
begin
begin
pos:=pos('../',url);
delete(url,1,begin
pos+2);
while i>=1do
begin
if baseurl='/' then
begin
dec(i);
break;
end;
dec(i);
end;
end;
dealwith:=copy(baseurl,1,i)+'/'+url;
end;
dealwith:=url;
end;

end.
 
TO 天真:
我试了但还是不行,我是想
通过当它为图片时保存到一个文件夹中,当为声音时保存到另一个文件夹中
TO kthy:
HTML 的TAG有什么意义呢,能具体说说,最好能付上例子谢谢!
 
functiondo
wnloadFile(Source, Dest: string): Boolean;
begin
try
Result:=UrlDownloadToFile(nil, PChar(source), PChar(Dest), 0, nil) = 0;
except
Result:=False;
end;
end;
你看这段代码就行!
 
这段代码就是把你要的东西保存到你要保存的地方
 
TO 天真:
我还是看不明白,能具体说说吗。
例如我想把属于图片的网页提取出来保存到picture文件夹中怎么实现?
 
你要保存的是HTML文件,还是相应的图片
楼上已经说得很清楚了,
1.已经得到了HTML文件,
2.对HTML文件进行分析,如是否包含'<img src' 及'.rm' 等此类的文字,
然后作相应的处理即可。
 
TO gong666:
是要保存相应的图片。
我就是不知道怎样对HTML文件进行分析,如怎样判断
包含'<img src' 及'.rm' 等此类的文字?
 
function ParseHTMLDocument(doc: IhtmlDocument2;
DownType: string;
AbsURL:string): TUrlArray;
var
b: IHTMLElement;
All: IHtmlElementCollection;
i: integer;
idx: OleVariant;
{do
wnType取值范围:image,css ,alltype;
}
AIndex:integer;
url:string;
begin
{这次导入地是IhtmlDocument2,是从html代码片断中导入地}
AIndex:=-1;
do
wnType:=lowercase(DownType);
if (DownType = 'image') or (DownType = 'alltype')
or (DownType = 'inner') then
begin
All :=do
c.images;
for i := 0 to All.length - 1do
begin
b := all.item(i, 0) as IHTMLElement;
inc(AIndex);
setlength(result,AIndex+1);
result[AIndex].Url :=(b as IHTMLImgElement).src;
result[AIndex].Title := b.innerText;
result[AIndex].Memo := (b as IHTMLImgElement).alt;
result[AIndex].FileType := 'image';
end;
end;
if (DownType = 'link') or (DownType = 'alltype') then
begin
All :=do
c.links;
for i := 0 to All.length - 1do
begin
b := all.item(i, 0) as IHTMLElement;
inc(AIndex);
setlength(result,AIndex+1);
result[AIndex].Url :=(b as IHTMLAnchorElement).href;
result[AIndex].Title := b.innerText;
result[AIndex].Memo := b.title;
result[AIndex].FileType := 'link';
end;
end;
//修改有错误。
if (DownType = 'flash') or (DownType = 'alltype') then
begin
All :=do
c.embeds;
for i := 0 to All.length - 1do
begin
b := all.item(i, 0) as IHTMLElement;
// b.setAttribute('type','aaa',0);
idx:=(b as DispHTMLEmbed).getAttribute('type',0);
idx:=b.getAttribute('type',0);
if (string(b.getAttribute('type',0)))='application/x-shockwave-flash' then
begin
inc(AIndex);
setlength(result,AIndex+1);
result[AIndex].Url :=(b as IHTMLEmbedElement).src;
result[AIndex].Title := b.innerText;
result[AIndex].Memo := '';
result[AIndex].FileType := 'flash';
end;
end;
end;
if (DownType = 'css') or (DownType = 'alltype')
or (DownType = 'inner') then
begin
for i := 0 todo
c.styleSheets.length - 1do
begin
idx := i;
url:=Doc.styleSheets.item(idx).href;
if url<>'' then
begin
inc(AIndex);
setlength(result,AIndex+1);
result[AIndex].Url := GetAbsUrl(AbsURL,url);
result[AIndex].Title := '';
result[AIndex].Memo := '';
result[AIndex].FileType := 'css';
end;
end;
end;
end;
 
TO soul:
"DownType" 不用定义吗
{do
wnType取值范围:image,css ,alltype;
}是各个值代表什么意思
 
哦,只是代码片断,核心块都有了,供参考。downtype只是控制一下解析出那些类型链接。
另外
UrlRecord = record
Url:string;
Title:string;
memo:string;
FileType:string;
Object:IDispatch;
FromElement:IHTMLElement;
end;
TUrlArray = Array of UrlRecord;
 
另外据我所知,IHtmlDocument2(WebBrowser1.Document).Body.innerHtml应该更加正确。
你自己可以试验一下,否则head部分取不到。body的innerHtml可能是一个特例。
 
谢谢各位!先结帖子,不懂再问
 
TO soul:
以下两句编译不过去
Object:IDispatch;
FromElement:IHTMLElement;
是要在use里加什么吗我加了mshtml
 
To:天真
result:=urldownloadtofile(nil,pchar(source),pchar(dest),0,nil)=0;
这个urldownloadfile是什么?能否告诉我?我现在要把图片保存起来,
您能否帮我一下?
我发了两个贴子,共100分。如果分不够,请说,我再加分。
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1251624
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1128932
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
892
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部