我正在使用组合的批处理和java脚本,我发现使用批处理文件从网站检索html,而我们解决的一个问题是没有返回所需的输出,就像我在firefox中使用url时出现的那样。
我用来拉取 html 的脚本是:
@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
rem Batch file will delegate all the work to the script engine
if not "%~1"=="" (
cscript //E:JScript "%~dpnx0" %1
)
rem End of batch area. Ensure batch ends execution before reaching
rem javascript zone
exit /b
@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');
// Retrieve the url parameter
var url = WScript.Arguments.Item(0)
// Make the request
http.open("GET", url, false);
http.send();
// If we get a OK from server (status 200), echo data to console
if (http.status === 200) WScript.StdOut.Write(http.responseText);
// All done. Exit
WScript.Quit(0);
我试图提供脚本的网址是《天方夜谭》]
或者替代地《天方夜谭》]
问题似乎是空格/+,因为我提供的其他网址都没有使用空格或+
我调用脚本来拉取 html 的方式是:
call callurl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
编辑:找到脚本来自的原始线程不使用浏览器从批处理文件中打开 URL
我所做的唯一更改是 Msxml2.XMLHTTP.6.0 更改为 MSXML2.ServerXMLHTTP.6.0,因为原始脚本由于我发现的安全性而无法加载网站。
在这种情况下,问题在于 Windows 脚本主机消耗了参数中包含的双引号。
npocmaka 已表明解决方案之一:对 url 中的引号进行编码。从我的角度来看,这是正确的(双引号是不安全的字符,应该进行编码)。
另一个解决方案是不将 URL 作为参数传递给脚本,而是将其存储在环境变量中,然后在 javascript 部分中从变量中检索值
@if (@This==@IsBatch) @then
@echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
rem Ensure we get a correct reference to current batch file
call :getFullBatchReference _f0
rem Batch file will delegate all the work to the script engine
if not "%~1"=="" (
set "URL=%~1"
cscript //nologo //E:JScript "%_f0%"
)
rem Ensure batch ends execution before reaching javascript zone
exit /b %errorlevel%
:getFullBatchReference returnVar
set "%~1=%~f0"
goto :eof
@end
// **** Javascript zone *****************************************************
// Instantiate the needed component to make url queries
var http = WScript.CreateObject('MSXML2.ServerXMLHTTP.6.0');
// Retrieve the url parameter from environment variable
var url = WScript.CreateObject('WScript.Shell')
.Environment('Process')
.Item('URL');
var exitCode = 0;
try {
// Make the request
http.open("GET", url, false);
http.send();
// If we get a OK from server (status 200), echo data to console
if (http.status === 200) {
WScript.StdOut.Write(http.responseText);
} else {
exitCode = http.status;
};
} catch (e) {
// Something failed
WScript.StdOut.Write('ERROR: ' + e.description );
exitCode = 1;
};
// All done. Exit
WScript.Quit( exitCode );
现在,它可以被称为
geturl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)