Using Input Parameters in Data Extraction Projects
輸入參數可以提供給web抓取項目,并且可以在項目內的所有腳本中使用。
輸入參數可用于與項目初始化腳本初始化一個web-scraping項目為一個特定的環境中,例如設置數據庫連接測試數據庫在測試環境中運行時,并設置連接到生產數據庫運行時在生產環境中。
可以在web抓取項目中定義特定的輸入參數。所有已定義的參數都可以有一個默認值。
image.png
如果項目有任何定義的輸入參數,那么當您從Visual Web Ripper 設計器中運行一個Web抓取項目時,就會出現一個參數進入屏幕。
image.png
如果您從命令行運行一個web抓取項目,您可以提供任意數量的已定義的和新的輸入參數。所有的參數都可以用于項目中的腳本。如果不為已定義的參數提供值,則使用默認值。
image.png
此時,您不能為使用內置調度程序運行的項目提供輸入參數。
使用輸入參數改變目標數據源 Using Input Parameters to Change the Destination Data Source
您可以配置一個項目初始化腳本,以使用輸入參數來更改目標數據源。該腳本只會更改所提供的數據庫連接屬性。例如,如果沒有提供用戶名,將使用默認的用戶名。
在Advanced Options窗口中添加了一個項目初始化腳本。
image.png
下面的腳本確定輸入參數是否存在,然后設置相應的數據庫連接屬性。
image.png
設置FixedValue內容來匹配輸入參數Setting FixedValue Content Equal to the Value of an Input Parameter
您可以使用一個簡單的內容轉換腳本來設置內容元素等于輸入參數的值。
using System;
using VisualWebRipper.Internal.SimpleHtmlParser;
using VisualWebRipper;
public class Script
{
public static string TransformContent(WrContentTransformationArguments args)
{
try
{
return args.InputParameters[ "par1" ];
}
catch (Exception exp)
{
args.WriteDebug(exp.Message);
return "Custom script error" ;
}
}
}
設置FormField內容來匹配輸入參數Setting FormField Content Equal to the Value of an Input Parameter
您可以使用一個簡單的輸入轉換腳本來設置一個FormField內容元素,它等于一個輸入參數的值。
using System;
using VisualWebRipper.Internal.SimpleHtmlParser;
using VisualWebRipper;
public class Script
{
//See help for a definition of WrInputTransformationArguments.
public static string TransformInput(WrInputTransformationArguments args)
{
try
{
return args.InputParameters[ "par1" ];
}
catch (Exception exp)
{
args.WriteDebug(exp.Message);
return "Custom script error" ;
}
}
}
設置項目開始URL來匹配輸入參數Setting the Project Start URL Equal to the Value of an Input Parameter
可以使用一個簡單的項目初始化腳本,將項目啟動URL設置為一個輸入參數的值。
using System;
using mshtml;
using VisualWebRipper;
public class Script
{
//See help for a definition of WrProjectInitializeArguments.
public static bool InitializeProject(WrProjectInitializeArguments args)
{
try
{
if (args.InputParameters.ContainsParameter( "url" ))
args.Project.StartUrl = args.InputParameters[ "url" ];
return true ;
}
catch (Exception exp)
{
args.WriteDebug(exp.Message);
return false ;
}
}
}