我正在使用一个批处理和Java脚本的组合,我发现使用batch file从网站检索的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);
我试图提供脚本的url是http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set= [ “阿拉伯之夜”]
或者可以selecthttp://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set= [ “阿拉伯之夜”]
问题似乎是空间/ +因为没有其他的url我喂它使用空间或+
我调用脚本来拉取html的方式是:
call callurl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
编辑:find脚本的原始线程从一个batch file中打开一个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"]"
像这样调用cscript:
cscript //E:JScript "%~dpnx0" "%~1"
我不认为空间需要编码,而是双引号( %22
),虽然这可能需要解析整个命令行( %*
),你可以尝试类似
setlocal enableDelayedExpansion set "link=%*" set "link=!link:"=%%22!" .... cscript //E:JScript "%~dpnx0" "%link%"
您也可以尝试命名参数 ,并将整个命令行传递给脚本。
只需更换空间 或带有URL编码空间
%20
加号+
。
例如http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set= [ “阿拉伯%20夜”]