脚本没有正确接收网址

red*_*h68 4 javascript windows url space batch-file

我正在使用我发现使用批处理文件从网站检索html的组合批处理和java脚本,并且当我在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);
Run Code Online (Sandbox Code Playgroud)

我试图提供脚本的网址http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set= [ "Arabian + Nights"]

或者替代地http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set= [ "Arabian Nights"]

问题似乎是空格/ +因为我提供的其他网址都没有使用空格或+

我调用脚本来拉取html的方式是:

call callurl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
Run Code Online (Sandbox Code Playgroud)

编辑:找到原始线程脚本来自打开URL而不使用批处理文件中的浏览器

只有我做的更改是Msxml2.XMLHTTP.6.0更改为MSXML2.ServerXMLHTTP.6.0因为原始脚本由于我发现的安全性而无法加载网站.

MC *_* ND 5

在这种情况下,问题是Windows脚本主机使用参数中包含的双引号.

npocmaka展示了其中一个解决方案:对网址中的引号进行编码.从我的观点来看,这是正确的(双引号是一个不安全的字符,应编码).

另一种解决方案是不将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 );
Run Code Online (Sandbox Code Playgroud)

现在,它可以被称为

geturl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]"
Run Code Online (Sandbox Code Playgroud)