web harvest 使用手册
简介
Web-Harvest是Web数据挖掘工具。提供一种方法收集目标网页和挖掘有用的数据。为此,使用XSLT,XQUERY,Regular Exp等技术。
其网址在http://web-harvest.sourceforge.net/。
挖掘数据也称为WebScraping或WebDataMining。每个挖掘过程是用户自定义的,通过执行基于xml的配置文件。
例如:
<xpath expression="//a[@shape='rect']/@href">
<html-to-xml>
<http url="http://www.somesite.com/"/>
</html-to-xml>
</xpath>
WebHarverst提供一组数据处理和控制流。Processor组合成管道,构成执行链。也提供了variable context。
目录
- 预定义的和用户定义的变量和对象
- Xml配置脚本使用的元素标记
- config - root element of configuration file
- empty - wraps processing with empty result
- text - forces textual representation of content
- var-def - defines variable
- var - return value of variable
- file - file read/write action
- http - sends HTTP request and retrieves response
- http-param - defines HTTP parameter for http processor
- http-header - defines HTTP header for http processor
- html-to-xml - cleans up HTML and transforms it to valid XML
- regexp - performs regular expression search/replace
- xpath - searches for XPath expression in XML source
- xquery - executes query on XML source
- xslt - applies XSL transformation on XML source
- script - use Java-based scripting language to write custom logic
- template - populate content with dynamic values
- case - conditional if/elseif/else branching
- loop - loops over list of values
- while - loops using logic condition
- function - defines user defined function
- return - returns value from user defined function
- call - calls execution of user defined function
- include - includes content of other configuration file
- try - handle exceptions
- exit - conditionally break the config execution
- GUI使用说明
- 参考文档
预定义的和用户定义的变量和对象
变量(函数) | 用途 |
| /n |
| /r |
| Space |
| /” |
| /’ |
| 退格键 |
| 返回系统日期(yyyyMMdd) |
| 返回系统时间() |
| 返回指定格式的日期或者时间 |
| 过滤文本使之符合xml标准 |
|
返回绝对链接。 |
| 定义新的用户变量,包括变量名和值。
|
| 判断变量是否存在 |
| 使用
|
http对象的方法
变量(函数) | 用途 |
| 脚本运行期间的httpclient对象,使用httpclient-3.x实现。 |
| 最后一次http会话返回的字节数 |
| 最后一次http会话的编码类型,如果它含有文本内容。 |
| 最后一次http会话的mime type |
| 最后一次http会话的消息头,使用map保存。
|
| 最后一次http会话的状态码 |
| 最后一次http会话的状态文本 |
| HTTP client接受到所有所有数据的字节数。 |
| HTTP client发起的所有http对话的次数。 |
Xml配置脚本使用的元素标记
config
脚本文件的根元素
属性名 | 是否必要 | 默认值 | 描述 |
charset | no | UTF-8 | 指定脚本文件的编码 |
scriptlang | no | beanshell | 指定运行脚本文件使用的scripting引擎。 可选值为beanshell, javascript和groovy. |
empty
一般用来模拟用,将返回结果设置为空。
text
将嵌入text标记中的变量转换成正则表达式字符串。
var-def
定义变量名。
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| 变量名 |
overwrite | no | true | 是否覆盖现存 |
var
返回变量值
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| 变量名 |
file
读写指定文件的内容。
属性名 | 是否必要 | 默认值 | 描述 |
action | no | read | 指定文件操作,包括:read,write,append |
path | yes |
| 指定文件路径,相对于工作目录的。 |
type | no | text | 指定文件类型,包括:text or binary |
charset | no | [Default charset for config] | 指定文本文件的编码类型,此参数对二进制文件无效。 |
http
发送http命令到指定的URL,接受http命令返回的结果。
http标签可以包含定义http参数和http头的标签,它们作为对http命令的补充,将按顺序执行。
属性名 | 是否必要 | 默认值 | 描述 |
url | yes |
| 指定URL地址 |
method | no | get | 指定HTTP方法:get or post |
charset | no | [Default charset for config] | 指定HTTP命令的文本编码格式 |
cookie-policy | no | [Default cookie policy of the HTTP client] | 指定管理cookies的方式。 可以设置为: browser, ignore, netscape, rfc_2109 and default. |
username | no |
| 安全验证需要的用户名 |
password | no |
| 安全验证需要的用户密码 |
http-param
设置HTTP参数。
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| 参数名 |
http-header
设置HTTP头
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| HTTP头名称 |
html-to-xml
将html的内容转为xhtml。使用HtmlCleaner实现。
此操作包含非常多的参数设置,对html转换成xml格式进行精确的控制。有兴趣的可以参考官方的文档(http://web-harvest.sourceforge.net/manual.php#html-to-xml)。
regexp
设置正则表达式。
属性名 | 是否必要 | 默认值 | 描述 |
replace | no | false | 是否使用替换功能 |
max | no |
| 匹配到的最大结果数 |
Regexp标签包含三个子标签。
regexp-pattern标签设置正则表达式。
regexp-source标签设置需要进行查询/匹配的文本。
regexp-result标签,在进行查询操作的时候,得到查询操作返回的结果;在执行替换操作时,设置进行替换的字符串。
xpath
使用xpath表达式搜索xml文档。
属性名 | 是否必要 | 默认值 | 描述 |
expression | yes |
| xpath表达式 |
xquery
使用xquery表达式查询xml文档。
xquery标签有两个子标签。
xq-param标签,设置xquery的参数值。它的属性列表如下:
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| xquery的参数名 |
type | no | node() | xquery的参数的类型。它的值包括:
|
xq-expression标签用于设置xquery表达式的构成。
xslt
使用xls样式表将xml文件转换成符合xlst的格式。
xlst标签使用stylesheet标签设置需要用到的样式表。
script
执行用指定scripting引擎的语言写成的代码。
org.webharvest.runtime.variables.Variable
可以使用的方法。
- String toString()
- byte[] toBinary()
- boolean toBoolean()
- int toInt()
- long toLong()
- double toDouble()
- double toDouble()
- Object[] toArray()
- java.util.List toList()
- Object getWrappedObject()
属性名 | 是否必要 | 默认值 | 描述 |
language | no | beanshell | 指定运行脚本文件使用的scripting引擎。 可选值为beanshell, javascript和groovy. |
return | no | Empty value | 返回值 |
template
对一段文本中使用${}包含的部分,利用指定scripting引擎的语言进行解析。
属性名 | 是否必要 | 默认值 | 描述 |
language | no | beanshell | 指定运行脚本文件使用的scripting引擎。 可选值为beanshell, javascript和groovy. |
case
执行判断。
case标签包含两个子标签。
if标签,设置判断条件,并且包含判断条件为真时,要执行的操作。
else标签,包含判断条件为假时,要执行的操作。
属性名 | 是否必要 | 默认值 | 描述 |
condition | yes |
| 判断表达式 |
loop
执行循环。
属性名 | 是否必要 | 默认值 | 描述 |
item | no |
| 设置item的名称 |
index | no |
| 设置索引值的名称 |
maxloops | no |
| 设置索引的界限 |
filter | no |
| 设置过滤器 它的值包括三种类型:
|
loop标签有两个子标签。
list标签设置item项的值列表。
body标签实现对每个item的操作。
while
执行另一种形式的循环。
属性名 | 是否必要 | 默认值 | 描述 |
condition | yes |
| 判断表达式 |
index | no |
| 初始化索引值,默认为1 |
maxloops | no |
| 最大循环数 |
function
定义用户函数
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| 用户函数名 |
return
定义函数返回值
call
执行用户函数。
属性名 | 是否必要 | 默认值 | 描述 |
name | yes |
| 用户函数名 |
include
包含外部文件。
属性名 | 是否必要 | 默认值 | 描述 |
path | yes |
| 外部文件路径 |
try
try标签执行包含异常处理的代码。它包含两个子标签。body标签包含要执行的代码;
catch标签包含异常处理的代码。
exit
带判断条件的中止程序操作。
属性名 | 是否必要 | 默认值 | 描述 |
condition | no |
| 判断表达式 |
message | no |
| 提示字符串 |
GUI使用说明
主界面
系统设置界面
参考文档
http://web-harvest.sourceforge.net/
附录
XPath
W3C的标准教程:http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html
另一版本的标准教程:http://www.w3school.com.cn/xpath/index.asp
XQuery
标准教程:http://www.w3school.com.cn/xquery/index.asp
xlst
标准教程:http://www.w3school.com.cn/xsl/index.asp
一些简单的例子
http://hi.baidu.com/%5Frenaisance/blog/category/Webharvest
图片传不上来,唉csdn的服务器,没有语言了。