windows计划任务自动生成静态首页收藏
作者:cmscn 日期:2009-02-27
indexplan.vbs: 脚本程序,调用ie组建执行生成首页的程序这里强调的是必须打开ie,要用到ie的组建,遨游好像不行。然后把这个脚本程序添加到windows计划任务里,设置好执行时间间隔,看你首页跟新的快慢了,自己把握。
view plaincopy to clipboardprint?
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
ie.navigate("http://localhost:80/createindex.asp")-这里在服务器换成你的域名指向你的生成文件
ie.visible=0
Set IE = Nothing
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
ie.navigate("http://localhost:80/createindex.asp")-这里在服务器换成你的域名指向你的生成文件
ie.visible=0
Set IE = NothingASP版本: view plaincopy to clipboardprint?
Server.ScriptTimeOut=9999999
Function BytesToBstr(body,Cset)'防止乱码函数
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = Nothing
End Function
Function GetHTTPPage(Url)'获取Html代码函数
err.clear
On Error Resume Next
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
GetHTTPPage = bytesToBSTR(Http.responseBody,"GB2312")
set http = Nothing
If Err Then
response.write err.description
Response.Write "<br><br><p align='center'><font color='red'><b>无法抓取本页面信息!!!</b></font></p>"
End If
End function
Url="http://www.xxxxx.com/index.asp"
strHtml=GetHTTPPage(Url)
filename=server.mappath("index.html")
if strHtml<>"" then'读取到首页源代码
set fso = Server.CreateObject("scripting.FileSystemObject")
if fso.FileExists(filename) then '静态首页存在,删除旧页面
fso.DeleteFile filename
set fout = fso.CreateTextFile(filename,true,False)'重新生成
fout.write strHtml
fout.close
set fout=nothing
set fso=nothing
else
set fout = fso.CreateTextFile(filename,true,False)
fout.write strHtml
fout.close
set fout=nothing
set fso=nothing
end if
end if
Server.ScriptTimeOut=9999999
Function BytesToBstr(body,Cset)'防止乱码函数
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = Nothing
End Function
Function GetHTTPPage(Url)'获取Html代码函数
err.clear
On Error Resume Next
dim http
set http=Server.createobject("Microsoft.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
GetHTTPPage = bytesToBSTR(Http.responseBody,"GB2312")
set http = Nothing
If Err Then
response.write err.description
Response.Write "<br><br><p align='center'><font color='red'><b>无法抓取本页面信息!!!</b></font></p>"
End If
End function
Url="http://www.xxxxx.com/index.asp"
strHtml=GetHTTPPage(Url)
filename=server.mappath("index.html")
if strHtml<>"" then'读取到首页源代码
set fso = Server.CreateObject("scripting.FileSystemObject")
if fso.FileExists(filename) then '静态首页存在,删除旧页面
fso.DeleteFile filename
set fout = fso.CreateTextFile(filename,true,False)'重新生成
fout.write strHtml
fout.close
set fout=nothing
set fso=nothing
else
set fout = fso.CreateTextFile(filename,true,False)
fout.write strHtml
fout.close
set fout=nothing
set fso=nothing
end if
end ifASP.NET版本: view plaincopy to clipboardprint?
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
public partial class CreatIndex : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//index.html页面存在先删除旧页面,然后生成新页面
if (File.Exists(@"E:\product\index.html"))
{
File.Delete(@"E:\product\index.html");
string URL = "http://www.xxxx.com";
string Content = GetHtmlContent(URL);
CreateIndex(Content);
}
//index.html页面不存在直接生成
else
{
string URL = "http://www.xxxx.com";
string Content = GetHtmlContent(URL);
CreateIndex(Content);
}
}
//生成index.html页面
protected void CreateIndex(string con)
{
string IndexPath = @"E:\product\index.html";
//StreamWriter sr = File.CreateText(@"E:\product\index.html");
StreamWriter sr = new StreamWriter(IndexPath, true, System.Text.Encoding.GetEncoding("GB2312"));
Response.Write("生成Index.html文件成功。生成时间:" + System.DateTime.Now);
sr.WriteLine(con);
sr.Close();
}
//读取网页源代码
private string GetHtmlContent(string Url)
{
string strHtmlContent = "";
try
{
//声明一个HttpWebRequest请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//连接超时时间
request.Timeout = 500000;
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamHtmlCode = response.GetResponseStream();
Encoding encoding = Encoding.GetEncoding("GB2312");
StreamReader streamReader = new StreamReader(streamHtmlCode, encoding);
strHtmlContent = streamReader.ReadToEnd();
}
catch
{
Response.Write("对不起出错了");
}
return strHtmlContent;
}
}
评论: 0 | 引用: 0 | 查看次数: 616
发表评论