脚本可以在许多地方使用,以定制Visual Web Ripper 行为或扩展标准功能。Visual Web Ripper 脚本为.NET功能可用C#,VB.Net, 或正则表达式。
脚本可以分为三类:
-
转换脚本
Transformation scripts
: 用于转换一段文本。例如,如果你从一个网页中提取一个地址,但是只需要邮政编码,你就可以把地址转换成邮政编码。您还可以将此过程视为提取子文本,但有时您可能不仅仅是提取子文本。这就是我们称之为变换的原因。 -
等待脚本
Wait Script
: 在处理动态内容时,在异步加载到网页上是很重要的。当AJAX回调完成时,Visual Web Ripper 无法自动确定,因此它等待页面上的内容发生变化。您可以使用等待脚本来指定要完成的AJAX回调需要多长时间。 -
扩展脚本
Extension Script
: 用于扩展 Visual Web Ripper的功能。后处理脚本是一个扩展脚本的示例,该脚本可以用于将一些定制的业务逻辑应用到提取的数据中。
脚本工具Script Utilities
脚本工具类使在Visual Web Ripper 脚本中访问数据库变得更容易。您可以定义一个共享脚本数据库连接,该数据库连接将为项目中的所有脚本提供访问。在运行脚本之前,Visual Web Ripper 会自动打开共享数据库连接,并在脚本完成运行后关闭连接。
您可以通过单击脚本编辑器中的database按钮来定义一个新的脚本数据库连接或编辑现有的数据库连接。
如果您已经定义了一个共享脚本数据库,那么Visual Web Ripper 将会将一个类WrSharedDatabase的实例传递给所有的脚本函数。WrSharedDatabase类有以下方法,不管您访问的数据库类型如何,它们都是相同的。
public void SetSql(string sql)
public void PrepareSql()
public void SetParameterTextValue(string parameterName, string value)
public void SetParameterDateTimeValue(string parameterName, DateTime value)
public void SetParameterIntValue(string parameterName, int value)
public void SetParameterDoubleValue(string parameterName, double value)
public void SetParameterNullValue(string parameterName)
public object ExecuteNonQuery()
public object ExecuteScalar()
public DataSet ExecuteDataSet()
public MySqlDataReader ExecuteMySqlDataReader()
public OleDbDataReader ExecuteOleDbDataReader()
public SqlDataReader ExecuteSqlDataReader()
如果想直接在数据库连接或命令对象上工作,可以使用WrSharedDatabase类的以下属性。
public SqlConnection SqlConnection;
public MySqlConnection MySqlConnection;
public OleDbConnection OleDbConnection;
public SqlCommand SqlCommand;
public MySqlCommand MySqlCommand;
public OleDbCommand OleDbCommand;
下面的代码片段显示了一个后处理脚本,该脚本使用一个共享脚本数据库将提取的数据写入数据库。
using System;
using mshtml;
using VisualWebRipper;
public class Script
{
//See help for a definition of WrPostprocessArguments.
public static bool Postprocess(WrPostprocessArguments args)
{
try
{
//First we set the SQL we'll use to insert data into the database table.
//The Database connection has already been set by defining a shared script
//database. Visual Web Ripper will automatically open and close the
//database connection.
args.Database.SetSql
("insert into properties (type,fors,title,description,area)
values (@type,@fors,@title,@description,@area)");
args.Database.PrepareSql();
//The first table contains the start URL. We only have one start URL in
//this project. ChildTableRows returns all rows in the first child table
foreach(WrDataRow finditRow in args.DataTable.ChildTableRows)
{
//The next child table is the page navigation data table
foreach(WrDataRow pageRow in finditRow.ChildTableRows)
{
//The last child table is where the property data is located
foreach(WrDataRow propertyRow in pageRow.ChildTableRows)
{
args.Database.SetParameterTextValue( "@type" ,
finditRow[ "type" ]);
args.Database.SetParameterTextValue( "@fors" ,
finditRow[ "fors" ]);
args.Database.SetParameterTextValue( "@title" ,
propertyRow[ "title" ]);
args.Database.SetParameterTextValue( "@description" ,
propertyRow[ "description" ]);
args.Database.SetParameterTextValue( "@area" ,
propertyRow[ "area" ]);
args.Database.ExecuteNonQuery();
}
}
}
return true ;
}
catch (Exception exp)
{
args.WriteDebug(exp.Message);
return false ;
}
}
}
导出脚本Export Script
导出脚本可以用来定制数据导出过程。可以使用导出脚本将提取的数据导出到数据库中的自定义数据结构,也可以将数据导出到自定义数据源。
从数据导出屏幕中选择导出脚本。
当您单击export script按钮时,导出脚本编辑器会打开。
The ExportData Method
一个导出脚本必须有一个方法,如下所示。
public static bool ExportData(WrExportArguments args)
{
try
{
//Place your export code here.
return true;
}
catch(Exception exp)
{
args.WriteDebug(exp.Message);
return true;
}
}
public static bool ExportData(WrExportArguments args)
ExportData()脚本方法必须有这个确切的名称和签名,所以只修改方法体,而不是方法签名。该方法返回true,表示成功或false表示失败。
Name | Type | Description |
---|---|---|
Project | WrProject | The current Visual Web Ripper project. |
ExportData | WrExportData | The extracted data. |
Database | WrSharedDatabase | An open database connection. * See Script Utilities for more information about shared script databases. |
InputDataRow | WrDataRow | The current input data row if an input data source has been defined. * See Using an Input Data Source for more information about input data sources. |
InputParameters | WrInputParameters | Input parameters for the current project. * See Using Input Parameters for more information about input parameters. |
WrExportArguments Properties
Name | Type | Description |
---|---|---|
Project | WrProject | The current Visual Web Ripper project. |
ExportData | WrExportData | The extracted data. |
Database | WrSharedDatabase | An open database connection. * See Script Utilities for more information about shared script databases. |
InputDataRow | WrDataRow | The current input data row if an input data source has been defined. * See Using an Input Data Source for more information about input data sources. |
InputParameters | WrInputParameters | Input parameters for the current project. * See Using Input Parameters for more information about input parameters. |
当在调试模式下运行一个项目时,您可以使用它来将信息写入调试窗口中。
Debugging an Export Script
调试一个导出脚本是很困难的,因为您不能设置断点并通过您的代码。您可以通过在WrExportArguments类中使用WriteDebug方法来执行简单的调试,返回false表示失败。如果单击数据导出屏幕上的Export Existing Data
按钮,脚本返回false,则消息框将显示最后的调试消息。
编写一个复杂的导出脚本需要一个更好的调试环境,在这个环境中,您可以遍历代码并查看变量的内容。您可以创建一个简单的。在您的正常开发环境中,例如Visual Studio中,NET测试应用程序。当配置。使用Visual Web Ripper API的NET应用程序,遵循以下步骤:
- 添加对Visual Web Ripper API DLLs的引用,它位于Visual Web Ripper安装文件夹中。您应该至少包括WebRipper.dll文件。
- 平台目标必须是x86。
- 应用程序必须使用 .NET framework v4
.NET 测试应用程序应该为您的项目加载导出数据,然后调用ExportData方法,如下所示。当您的测试应用程序工作时,您可以直接将ExportData方法复制到Visual Web Ripper 脚本编辑器中。
using System;
using System.Collections.Generic;
using System.Text;
using VisualWebRipper;
namespace DataExport
{
class Program
{
static void Main(string[] args)
{
WrProject project = WrProject.LoadByName("projectName");
WrExportData data = project.OpenExportedData();
WrExportArguments exportArgs = new WrExportArguments(data, project);
ExportData(exportArgs);
}
public static bool ExportData(WrExportArguments args)
{
try
{
//First we set the SQL we'll use to insert data into the database table.
//The Database connection has already been set by defining a shared script
//database. Visual Web Ripper will automatically open and close the
//database connection.
args.Database.SetSql("insert into properties (type,fors,title,description,area) values (@type,@fors,@title,@description,@area)");
args.Database.PrepareSql();
//Loop htough all eth export tables
foreach (WrExportTableDefinition table in args.ExportData.TablesDefinitions.Tables)
{
//Open a data reader for the current table
WrExportTableReader reader = args.ExportData.GetTableReader(table.TableName);
try
{
//Loop though all rows in the current data table and write them to the target database.
while (reader.Read())
{
args.Database.SetParameterTextValue("@type",
reader.GetStringValue("type"));
args.Database.SetParameterTextValue("@fors",
reader.GetStringValue("fors"));
args.Database.SetParameterTextValue("@title",
reader.GetStringValue("title"));
args.Database.SetParameterTextValue("@description",
reader.GetStringValue("description"));
args.Database.SetParameterTextValue("@area",
reader.GetStringValue("area"));
args.Database.ExecuteNonQuery();
}
}
finally
{
reader.Close();
}
}
return true;
}
catch (Exception exp)
{
args.WriteDebug(exp.Message);
return false;
}
}
}
}