批量下载最新的文件(其中文件名更改在新版本)从http网站



我需要一个批量从HTTP网站(http://www.rarlab.com/download.htm)下载文件。从这个网站,我只需要32位和64位英语的最新版本程序,该程序总是列在本网站的顶部。

问题1:网站上有两个以上的文件可供下载

问题2:每个新版本的文件名都改变

我怎么能下载这2个文件(最新版本)不知道确切的文件名(没有先访问网页找到文件名)??

也许我可以使用wget, curl或aria2的任务,但我不知道参数/选项。

谁能帮我解决这个问题?

(请只批处理解决方案-没有vbs, java, jscript, powershell等)

谢谢。

对不起,我忘了说我用的是32位的windows 7。我更喜欢批处理,因为脚本应该能够在所有的windows版本上运行,而不必为不同的windows版本下载额外的程序或资源工具包(如powershell,必须为windows xp等下载)-因为我只理解批处理脚本。

这是一个批处理+ JScript的混合脚本。我知道你说过没有vbs, java, jscript等,但是你将会有一个非常困难的时间用纯批处理抓取HTML。但这确实符合你的其他标准——在所有Windows版本上运行,而不必依赖可选软件(如powershell或。net)。*使用JScript的XMLHTTP对象,您甚至不需要第三方应用程序来获取web内容。

至于不理解JScript,除了一些专有的ActiveX对象,它就像JavaScript。如果您不熟悉JavaScript或正则表达式,我添加了大量的注释来帮助您。希望我没有费心评论的东西是很明显的。

更新

脚本现在检测系统语言环境,将其与WinRAR下载页面上的语言进行匹配,并下载该语言版本。

无论如何,用.bat扩展名保存它,并像运行其他批处理脚本一样运行它。

@if (@a==@b) @end /*
:: batch script portion
@echo off
setlocal
set "url=http://www.rarlab.com/download.htm"
set /p "savepath=Location to save? [%cd%] "
if "%savepath%"=="" set "savepath=%cd%"
cscript /nologo /e:jscript "%~f0" "%url%" "%savepath%"
goto :EOF
:: JScript portion */
// populate translation from locale identifier hex value to WinRAR language label
// http://msdn.microsoft.com/en-us/library/dd318693.aspx
var abbrev={}, a=function(arr,val){for(var i=0;i<arr.length;i++)abbrev[arr[i]]=val};
a(['1401','3c01','0c01','0801','2001','4001','2801','1c01','3801','2401'],'Arabic');
a(['042b'],'Armenian');
a(['082c','042c'],'Azerbaijani');
a(['0423'],'Belarusian');
a(['0402'],'Bulgarian');
a(['0403'],'Catalan');
a(['7c04'],'Chinese Traditional');
a(['0c04','1404','1004','0004'],'Chinese Simplified');
a(['101a'],'Croatian');
a(['0405'],'Czech');
a(['0406'],'Danish');
a(['0813','0413'],'Dutch');
a(['0425'],'Estonian');
a(['040b'],'Finnish');
a(['080c','0c0c','040c','140c','180c','100c'],'French');
a(['0437'],'Georgian');
a(['0c07','0407','1407','1007','0807'],'German');
a(['0408'],'Greek');
a(['040d'],'Hebrew');
a(['040e'],'Hungarian');
a(['0421'],'Indonesian');
a(['0410','0810'],'Italian');
a(['0411'],'Japanese');
a(['0412'],'Korean');
a(['0427'],'Lithuanian');
a(['042f'],'Macedonian');
a(['0414','0814'],'Norwegian');
a(['0429'],'Persian');
a(['0415'],'Polish');
a(['0816'],'Portuguese');
a(['0416'],'Portuguese Brazilian');
a(['0418'],'Romanian');
a(['0419'],'Russian');
a(['7c1a','1c1a','0c1a'],'Serbian Cyrillic');
a(['181a','081a'],'Serbian Latin');
a(['041b'],'Slovak');
a(['0424'],'Slovenian');
a(['2c0a','400a','340a','240a','140a','1c0a','300a','440a','100a','480a','080a','4c0a','180a','3c0a','280a','500a','0c0a','040a','540a','380a','200a'],'Spanish');
a(['081d','041d'],'Swedish');
a(['041e'],'Thai');
a(['041f'],'Turkish');
a(['0422'],'Ukranian');
a(['0843','0443'],'Uzbek');
a(['0803'],'Valencian');
a(['042a'],'Vietnamese');
function language() {
    var os = GetObject('winmgmts:').ExecQuery('select Locale from Win32_OperatingSystem');
    var locale = new Enumerator(os).item().Locale;
    // default to English if locale is not in abbrev{}
    return abbrev[locale.toLowerCase()] || 'English';
}
function fetch(url) {
    var xObj = new ActiveXObject("Microsoft.XMLHTTP");
    xObj.open("GET",url,true);
    xObj.setRequestHeader('User-Agent','XMLHTTP/1.0');
    xObj.send('');
    while (xObj.readyState != 4) WSH.Sleep(50);
    return(xObj);
}
function save(xObj, file) {
    var stream = new ActiveXObject("ADODB.Stream");
    with (stream) {
        type = 1; // binary
        open();
        write(xObj.responseBody);
        saveToFile(file, 2); // overwrite
        close();
    }
}
// fetch the initial web page
var x = fetch(WSH.Arguments(0));
// make HTML response all one line
var html = x.responseText.split(/r?n/).join('');
// create array of hrefs matching *.exe where the link text contains system language
var r = new RegExp('<a\s*href="[^"]+\.exe(?=[^\/]+' + language() + ')', 'g');
var anchors = html.match(r)
// use only the first two
for (var i=0; i<2; i++) {
    // use only the stuff after the quotation mark to the end
    var dl = '' + /[^"]+$/.exec(anchors[i]);
    // if the location is a relative path, prepend the domain
    if (dl.substring(0,1) == '/') dl = /.+://[^/]+/.exec(WSH.Arguments(0)) + dl;
    // target is pathfilename
    var target=WSH.Arguments(1) + '\' + /[^/]+$/.exec(dl)
    // echo without a new line
    WSH.StdOut.Write('Saving ' + target + '... ');
    // fetch file and save it
    save(fetch(dl), target);
    WSH.Echo('Done.');
}

更新2

这里是相同的脚本,有一些小的调整,使它也可以检测Windows的架构(32/64位),并且只下载一个安装程序,而不是两个:

@if (@a==@b) @end /*
:: batch script portion
@echo off
setlocal
set "url=http://www.rarlab.com/download.htm"
set /p "savepath=Location to save? [%cd%] "
if "%savepath%"=="" set "savepath=%cd%"
cscript /nologo /e:jscript "%~f0" "%url%" "%savepath%"
goto :EOF
:: JScript portion */
// populate translation from locale identifier hex value to WinRAR language label
// http://msdn.microsoft.com/en-us/library/dd318693.aspx
var abbrev={}, a=function(arr,val){for(var i=0;i<arr.length;i++)abbrev[arr[i]]=val};
a(['1401','3c01','0c01','0801','2001','4001','2801','1c01','3801','2401'],'Arabic');
a(['042b'],'Armenian');
a(['082c','042c'],'Azerbaijani');
a(['0423'],'Belarusian');
a(['0402'],'Bulgarian');
a(['0403'],'Catalan');
a(['7c04'],'Chinese Traditional');
a(['0c04','1404','1004','0004'],'Chinese Simplified');
a(['101a'],'Croatian');
a(['0405'],'Czech');
a(['0406'],'Danish');
a(['0813','0413'],'Dutch');
a(['0425'],'Estonian');
a(['040b'],'Finnish');
a(['080c','0c0c','040c','140c','180c','100c'],'French');
a(['0437'],'Georgian');
a(['0c07','0407','1407','1007','0807'],'German');
a(['0408'],'Greek');
a(['040d'],'Hebrew');
a(['040e'],'Hungarian');
a(['0421'],'Indonesian');
a(['0410','0810'],'Italian');
a(['0411'],'Japanese');
a(['0412'],'Korean');
a(['0427'],'Lithuanian');
a(['042f'],'Macedonian');
a(['0414','0814'],'Norwegian');
a(['0429'],'Persian');
a(['0415'],'Polish');
a(['0816'],'Portuguese');
a(['0416'],'Portuguese Brazilian');
a(['0418'],'Romanian');
a(['0419'],'Russian');
a(['7c1a','1c1a','0c1a'],'Serbian Cyrillic');
a(['181a','081a'],'Serbian Latin');
a(['041b'],'Slovak');
a(['0424'],'Slovenian');
a(['2c0a','400a','340a','240a','140a','1c0a','300a','440a','100a','480a','080a','4c0a','180a','3c0a','280a','500a','0c0a','040a','540a','380a','200a'],'Spanish');
a(['081d','041d'],'Swedish');
a(['041e'],'Thai');
a(['041f'],'Turkish');
a(['0422'],'Ukranian');
a(['0843','0443'],'Uzbek');
a(['0803'],'Valencian');
a(['042a'],'Vietnamese');
function language() {
    var os = GetObject('winmgmts:').ExecQuery('select Locale from Win32_OperatingSystem');
    var locale = new Enumerator(os).item().Locale;
    // default to English if locale is not in abbrev{}
    return abbrev[locale.toLowerCase()] || 'English';
}
function fetch(url) {
    var xObj = new ActiveXObject("Microsoft.XMLHTTP");
    xObj.open("GET",url,true);
    xObj.setRequestHeader('User-Agent','XMLHTTP/1.0');
    xObj.send('');
    while (xObj.readyState != 4) WSH.Sleep(50);
    return(xObj);
}
function save(xObj, file) {
    var stream = new ActiveXObject("ADODB.Stream");
    with (stream) {
        type = 1; // binary
        open();
        write(xObj.responseBody);
        saveToFile(file, 2); // overwrite
        close();
    }
}
// fetch the initial web page
var x = fetch(WSH.Arguments(0));
// make HTML response all one line
var html = x.responseText.split(/r?n/).join('');
// get OS architecture (This method is much faster than the Win32_Processor.AddressWidth method)
var os = GetObject('winmgmts:').ExecQuery('select OSArchitecture from Win32_OperatingSystem');
var arch = /d+/.exec(new Enumerator(os).item().OSArchitecture) * 1;
// get link matching *.exe where the link text contains system language and architecture
var r = new RegExp('<a\s*href="[^"]+\.exe(?=[^\/]+' + language() + '[^<]+' + arch + '\Wbit)');
var link = r.exec(html)
// use only the stuff after the quotation mark to the end
var dl = '' + /[^"]+$/.exec(link);
// if the location is a relative path, prepend the domain
if (dl.substring(0,1) == '/') dl = /.+://[^/]+/.exec(WSH.Arguments(0)) + dl;
// target is pathfilename
var target=WSH.Arguments(1) + '\' + /[^/]+$/.exec(dl)
// echo without a new line
WSH.StdOut.Write('Saving ' + target + '... ');
// fetch file and save it
save(fetch(dl), target);
WSH.Echo('Done.');

最新更新