脚本

脚本可以在许多地方使用,以定制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按钮来定义一个新的脚本数据库连接或编辑现有的数据库连接。


image.png

如果您已经定义了一个共享脚本数据库,那么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


导出脚本可以用来定制数据导出过程。可以使用导出脚本将提取的数据导出到数据库中的自定义数据结构,也可以将数据导出到自定义数据源。

从数据导出屏幕中选择导出脚本。


image.png

当您单击export script按钮时,导出脚本编辑器会打开。

image.png

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;
            }
        }
    }
}


































©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • Content Transformation 内容转换脚本用于在从网页中提取内容后转换内容。内容转换通常用于HTM...
    游侠儿evil阅读 737评论 0 0
  • 脚本语言又被称为扩建的语言,或者动态语言,是一种编程语言,用来控制软件应用程序,脚本通常以文本(如ASCII)保存...
    Daimer阅读 1,195评论 0 10
  • 官网原文:本章原文 建议打开原版对照着英文版同时阅读。官网原文:在git上阅读 建议打开原版对照着英文版同时阅读。...
    阿龙学区块链阅读 963评论 0 2
  • 本文全面系统地介绍了shell脚本调试技术,包括使用echo, tee, trap等命令输出关键信息,跟踪变量的值...
    liuzg0734阅读 877评论 0 14
  • JMeter录制 JMeter自身提供了http代理方式进行录制,原理是解析网络数据包,按Http协议包装秤Htt...
    ottol阅读 2,341评论 1 6