使用hta解析网页,并上传结果至FTP
HTA介绍详见http://en.wikipedia.org/wiki/HTML_Application本文分三部分,记录了碰到的问题与解决方法
一、HTA编写。
利用HTA进行解析网页信息,类似的工具应该不少,之所以使用hta 主要是由于轻量级,容易执行。但是由于使用比较少,处理问题时碰到的问题比较多:
1. 判断网页是否打开(更多需求参照 selenium实现)
一般开发web页面时,常用的页面事件为body的onload,或者Jquery插件的$(document).ready(). 但是在HTA中你无法通过打开页面的body来注册事件,因为无法预知body对象是否已经创建或者onload结束了。下面是我的处理,
function doWaitPageLoad(objName,functionName){
setTimeout("detectWaitPageLoad('"+objName+"','"+functionName+"')", 1000);
}
function detectWaitPageLoad(objName,functionName){
var obj = execCmd(objName);
if(obj.childwin && obj.childwin.document && obj.childwin.document.readyState=='complete'){
execCmd(objName+'.'+functionName+'()');
}
else{
setTimeout("detectWaitPageLoad('"+objName+"','"+functionName+"')",500);
}
}
function execCmd(cmd){
return eval("("+cmd+")");
}
此处理的核心部分就是,
obj.childwin && obj.childwin.document && obj.childwin.document.readyState
,
obj.chidwin
就是你打开的窗口对象,
obj.childwin = window.open(url, "childwin");
2. ActiveXObject调用
function saveFile(strFullPath, strContent) {
var fso = new ActiveXObject( "Scripting.FileSystemObject" );
var flOutput = fso.CreateTextFile( strFullPath, true );
flOutput.Write( strContent );
flOutput.Close();
}
function deleteFile(strFullPath){
var deleteFileObject = new ActiveXObject( "Scripting.FileSystemObject" );
if(deleteFileObject.FileExists( strFullPath)){
delefilepath=deleteFileObject.GetFile(strFullPath);
delefilepath.Delete ();
}
}
function saveFileWithUTF(strFullPath,strContent){
deleteFile(strFullPath);
var fso = new ActiveXObject("adodb.stream");
fso.Type = 2;
fso.mode = 3;
fso.charset = "utf-8";
fso.open();
fso.WriteText(strContent);
fso.SaveToFile(strFullPath);
fso.Close();
}
function uploadToFTP(){
var shellScript = new ActiveXObject( "WScript.Shell" );
//shellScript.Run("cmd.exe 'fileupload.bat'");
shellScript.Run("fileuploadwithscriptftp.bat");
}
1) 保存js文件,第一种方式
Scripting.FileSystemObject无法指定文件编码格式,然后换成了
adodb.stream定义成特定格式UTF-8编码进行保存文件
2)执行bat脚本使用WScript.Shell。
二、上传ftp
尝试了好几种方式,最终使用了破解付费软件才搞定:
1. 第一次使用MS-DOS的ftp, 结果因为ftp服务器使用的是PASV模式,无法建立数据通道
@echo open www.example.com>>t
@echo accountName>>t
@echo password>>t
@echo cd serverPath>>t
@echo ascii>>t
@echo literal pasv>>t
@echo put localfile>>t
@echo bye>>t
@ftp -i -s:t
@del t
2. 使用LFTP for window版本,结果可能是因为ftp server精致ssl连接导致还是上传失败,
XX.bat
lftp\lftp.exe -f yourscriptfile
yourscriptfile:
open -u 'accout name','password' ftp.example.com
cd yourserverpath
lcd localpath
mput *
quit
3. 最后使用ScriptFTP, 貌似很顺畅
XX.bat
ScriptFTP\ScriptFTP_console.exe upload.ftp
upload.ftp
# Connect to FTP server
OPENHOST("ftp.example.com","username","password")
# Change current remote directory to
CHDIR("/server path")
# Send all the files and subdirectories in C:\MyDir to the server
PUTFILE("dir\*.*",SUBDIRS)
# Transfer finished, close the connection
CLOSEHOST
三、添加至window tasks
待续....
页:
[1]