2016-09-20 114 views
3

我使用了一个批处理和Java脚本的组合,我发现使用批处理文件从网站中检索html,而我们的一个地址没有返回所需的输出,因为它在我使用url Firefox浏览器。脚本没有正确地收到URL

我使用拉HTML的脚本是:

@if (@[email protected]) @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=[“阿拉伯+夜”]

或alternativly http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=[“一千零一夜” ]

问题似乎是空间/ +为没有其它URL我喂它正在使用的空间或+

的WA Ÿ我打电话的脚本拉HTML是:

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

编辑:找到原来的线程的脚本是从Open a URL without using a browser from a batch file

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

回答

4

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

npocmaka显示one of the solutions:在url中对引号进行编码。从我的角度来看,这是正确的(双引号是不安全的字符,应该编码)。

另一种解决方案是不将URL作为参数传递给脚本,而是将其存储在一个环境变量,然后在JavaScript部分检索变量

@if (@[email protected]) @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"]" 
0
+0

该脚本似乎不正确地解释%20可能是因为它从一个批处理文件提供? –

+2

您需要用另一个'%'来转义'%',所以请使用'%% 20'。 – SomethingDark

+0

其返回这样的网站http://gatherer.wizards.com/Pages/Search/Default.aspx?output = spoiler&method = visual&action = advanced&set = [%22Arabian %% 20Nights%22] 而不是像这样http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set= [ %22Arabian%20Nights%22] ill编辑第一篇文章,以显示我如何从批处理文件调用脚本也许我会打电话给它错误 –

2

呼叫CSCRIPT这样的:

cscript //E:JScript "%~dpnx0" "%~1" 

我不认为空间需要被编码而是双引号(带%22),虽然这可能需要解析整个命令行(%*),你可以尝试这样

setlocal enableDelayedExpansion 
set "link=%*" 
set "link=!link:"=%%22!" 
.... 
cscript //E:JScript "%~dpnx0" "%link%" 

您还可以尝试named arguments和整个传递命令行的脚本。

+0

脚本是javascript和批处理的混合体,如第一篇文章所示,它被保存为callurl.cmd,因为文章中提到了它的名字 –

+0

我想他是指批处理文件你移动到JS部分。 – geisterfurz007

+0

@ geisterfurz007 - 是的。我知道这个技巧。问题在于双引号,脚本认为有两个参数。 – npocmaka