2016-09-20 112 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