获取网页快照

简介: unit uWebCracker; interface uses mshtml,SHdocvw,classes,SysUtils,StrUtils; const MAXPAGECOUNT=20; type TWebPageRecord=record URL:string; Titl...

unit uWebCracker;

interface

uses mshtml,SHdocvw,classes,SysUtils,StrUtils;

const

MAXPAGECOUNT=20;

type

TWebPageRecord=record

URL:string;

Title:string;

Text:string;

end;

type

TWebCracker=class(TObject)

private

FWebPageRecordArray:array[0..MAXPAGECOUNT-1] of TWebPageRecord;

FWebPageCount:integer;

public

constructor Create;

destructor Free;

procedure SnapShot;

function GetWebText(AIndex:integer):string;

function GetWebTitle(AIndex:integer):sttring;

function GetWebURL(AIndex:integer):string;

procedure Clear;

procedure Refresh;

function GetWebPageCount:integer;

end;

implementation

constructor TWebCracker.Create;

begin

inherited Create;

FWebPageCount:=0;

end;

destructor TWebCracker.Free;

begin

clear;

inherited Free;

end;

procedure TWebCracker.SnapShot;

const

ERRORNOTLOADCOMPLETE='可能打开的网页还没有完全加载,请当所有的网页下载完后再刷新!'

var

ShellWindow:IShellWindow;

WebBrowser:IWebBrower2;

I,ShellWindowCount:integer;

HTMLDocument:IHTMLDocument2;

URL:string;

WebPageRecord:TWebPageRecord;

begin

FWebPageCount :=0;

ShellWindow:=CoShellWindow.Create;

ShellWindowCount :=ShellWindow.Create;

if ShellWindowCount>MAXPAGECOUNT then

ShellWindowCount:=MAXPAGECOUNT;

for i:=0 to ShellWindowCount-1 do

begin

WebBrowser:=ShellWindow.Item(I) as IWebBrowser2;

URL:=WebBrowser.LocationURL;

if (WebBrowser<>nil) and (not IsLocationFile(URL)) then

begin

try

HTMLDocument :=WebBrowser.Document as IHTMLDocument2;

WebPageRecord.URL :=URL;

WebPageRecord.Title :=HTMLDocument.title;

WebPageRecord.Text :=HTMLDocument.body.outerText;

FWebPageRecordArray[I] :=WebPageRecord;

Inc(FWebPageCount);

except

on Exception do

raise Exception.Create(ERRORNOTLOADCOMPLETE);

end;

end;

ShellWindow :=nil;

end;

end;

function TWebCracker.GetWebText(AIndex:integer):string;

begin

Result :=FWebPageRecordArray[AIndex].Text;

end;

function TWebCracker.GetWebTitle(AIndex:integer):string;

begin

Result :=FWebPageRecordArray[AIndex].Title;

end;

function TWebCracker.GetWebURL(AIndex:integer):string;

begin

Result :=FWebPageRecordArray[AIndex].URL;

end;

procedureTWebCracker.Clear;

begin

FWebPageCount :=0;

end;

procedureTWebCracker.Refresh;

begin

self.Snapshot;

end;

functionTWebCracker.GetWebPageCount:integer;

begin

Result :=FWebPageCount;

end;

相关文章
|
SQL XML 算法
揭秘!我收藏夹里的常用网站
揭秘!我收藏夹里的常用网站
190 0
|
开发工具
【资料分享 | 浏览器收藏夹】
【资料分享 | 浏览器收藏夹】
107 0
【资料分享 | 浏览器收藏夹】
|
SQL PHP
dedecms友情链接显示不全解决方法
dedecms友情链接显示不全解决方法
116 0
|
搜索推荐
分享7个超棒的免费高质量图标搜索引擎
在工作中,我们经常需要用到图标素材。你也许能搜到很多的图标资源网站,但要找到免费的高质量图标却很难,这就是为什么我今天要与大家分享这些优秀的免费质量图标搜索引擎,相信在这些网站你能找到你要的东西。如果你知道更多更好的网站,欢迎与我们分享!
303 0
分享7个超棒的免费高质量图标搜索引擎
|
数据采集 Python Windows
python爬虫-抓取百度贴吧帖子图片
本爬虫可以爬取百度贴吧帖子中的图片,代码有待完善,欢迎大家指教! 出处:https://github.com/jingsupo/python-spider/blob/master/day03/07tieba.
1001 0