获取网页快照

简介: 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;

相关文章
|
数据采集 安全 前端开发
Hugo 网站优化(6): 博客图片不能显示, 全怪 Adblock 给我屏蔽了
Hugo 网站优化(6): 博客图片不能显示, 全怪 Adblock。 Referrer Policy: no-referrer-when-downgrade
217 0
Hugo 网站优化(6): 博客图片不能显示, 全怪 Adblock 给我屏蔽了
|
SQL XML 算法
揭秘!我收藏夹里的常用网站
揭秘!我收藏夹里的常用网站
194 0
|
开发工具
【资料分享 | 浏览器收藏夹】
【资料分享 | 浏览器收藏夹】
109 0
【资料分享 | 浏览器收藏夹】
|
SQL PHP
dedecms友情链接显示不全解决方法
dedecms友情链接显示不全解决方法
122 0
|
搜索推荐
分析百度不收录内页的原因及解决办法
各位在做SEO工作的时候想必都遇到过首页收录了内容页不收录也不知道是什么原因,也不知如何处理,今天泽民给大家分析下为什么内容页收录比较慢。 正常情况下新站内页收录时间会长一些,大概在首页收录后半个月到一个月开始收录内容,所以不讨论新站的内页收录。
1010 0
|
SEO 自然语言处理 搜索推荐