【问题标题】:Script not recieving url properly脚本未正确接收 url
【发布时间】:2017-01-28 00:43:07
【问题描述】:

我正在使用组合的批处理和 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);

我尝试输入脚本的网址是 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=["一千零一夜"]

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

我调用脚本来拉取 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,因为原始脚本由于我发现的安全性而无法加载站点。

【问题讨论】:

    标签: javascript windows batch-file url space


    【解决方案1】:

    只需将空格 或加号 + 替换为 URL 编码空格 %20

    例如http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian%20Nights"]

    【讨论】:

    • 脚本似乎没有正确解释 %20 可能是因为它是从批处理文件中输入的?
    • 您需要使用另一个% 转义%,因此请改用%%20
    • 它像这样gatherer.wizards.com/Pages/Search/…而不是像这样gatherer.wizards.com/Pages/Search/…返回站点,我将编辑第一篇文章以显示我如何从批处理文件中调用脚本可能我调用它错误
    • @SomethingDark - 链接中也有双引号。我不认为空间是问题
    【解决方案2】:

    像这样调用 cscript:

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

    我认为不需要对空格进行编码,而是需要对双引号(%22)进行编码,尽管这可能需要解析整个命令行(%*),您可以尝试类似

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

    您也可以尝试使用named arguments 并将整个命令行传递给脚本。

    【讨论】:

    • 该脚本是 javascript 和批处理之间的混合体,如第一篇文章所示,它被保存为 callurl.cmd,因为帖子称它为它命名
    • 我猜他的意思是在你移动到 JS 部分的批处理文件中。
    • @geisterfurz007 - 是的。我知道这种技术。问题出在双引号中,脚本认为有两个参数。
    • 更改导致以下输出 C:\Users\reddeath68\Desktop\test>test.bat 'method' 不是内部或外部命令、可运行程序或批处理文件。 'action' 不是内部或外部命令、可运行程序或批处理文件。该命令的语法不正确。 C:\Users\reddeath68\Desktop\test\callurl.cmd(39, 1) Microsoft JScript 编译错误:语法错误
    • 您的方法是正确的(将 URL 中的引号更改为 %22),但使用命名参数不会解决任何问题。问题的原因是 windows 脚本主机删除了参数中的任何双引号,因此内部 javascript 代码不会将它们包含在导致请求失败的 URL 中
    【解决方案3】:

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

    npocmaka 已显示 one of the solutions: 对 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"]"
    

    【讨论】:

    • 感谢您的帮助,但我仍然无法获得输出我想要的 html 应该列出一堆带有文本的行,例如
    • @reddeath68,测试为geturl.cmd "http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=visual&action=advanced&set=["Arabian+Nights"]",我得到了指示的输出。你得到了什么?
    • pastebin.com/YRdJrsnS 是我得到的输出,值得注意的是,当添加到 cmd 行时,它将 url 添加为“gatherer.wizards.com/Pages/Search/…”注意双问号
    • @reddeath68,由于某种原因(我不知道为什么),当从上一条注释复制命令行时,有不可见的字符。我已将其包含在答案中。你能测试一下吗?
    • 这样做是为了首先将隐藏字符传递给脚本?
    猜你喜欢
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多