基础规则
2022年10月27日大约 2 分钟
本文主要介绍基本的取数据元素的方法。
取 html 元素
可以根据元素的 class 或 id 使用 css选择器 获取,也可以通过 XPath 路径获取元素。若使用了 js,最后应输出 Array 对象。
根据元素的 class 或 id 取,等同于 @css:
需以 . 或 # 开头,紧跟 css 选择器代码。
.class
#id
.class1.class2
.class1 .class2
.class a@text
.class a@textNodes
.class@html
.class@innerHtml
.class@outerHtml
#id a@href
参考: 《CSS选择器参考手册》
根据元素路径取,等同于 @xpath:
需以 / 开头,紧跟元素 xpath 路径表达式。
//*[@id="resultDiv"]/tr
//*[@class="title"]/text()
取元素规则后面紧跟## {查找内容} ## {替换为} ,可实现对元素内容进行替换的功能。此方式同样适用于 @css, @json 规则。
规则:rule##replaceRegex##replacement##replaceFirst
示例: 取出类名为title的元素的href属性内容,将将book替换为chapter
//*[@class="title"]/@href##book##chapter
使用 @css:[CSS选择器]
@css:.class
@css:#id a@href
使用 @xpath:[路径表达式]
@xpath://a
@xpath:/a/@href
@xpath: /a/text()
参考:
取 Json 数据
可以直接使用 $ 开头或 @json: 来获取 json 内容。
使用 $ 开头,省略 @json:
$..item.*
$.data.content[*]
$.name
使用 @json:
@json:$.books.*
@json:$.name
参考:
正则表达式取数据
可以使用 : 开头或 @regexp: 或 @regexp: 来使用正则功能取数据。
使用 :[正式表达式] 或 @regexp: 或 @regex:
:h3[\s\S]*?h3
@regex:h3[\s\S]*?h3
@regexp:h3[\s\S]*?h3
参考: 《正则表达式简介》
使用自定义符号 AND
添加自定义符号 AND:
@regex:<div>[\w\W]*</div>AND<li>.*?</li>
会先匹配前面的内容,然后在前一个 AND 的结果中匹配后面的内容。
使用正向/反向肯定/否定预查
可以使用正向/反向肯定/否定预查 (?<=pattern) 或 (?pattern) 或 (?!pattern) 或 (?<!pattern) 来定位内容。
例子:
@regex:r'(?<=href=")[^"]*'
匹配 @match: 规则
可以使用 @match: 规则来匹配 字符串 String、元素Element、Map 或 List 内容,并得到由 (两个空格)连接的字符串。
格式如下:
@match:[regex]
@match:[regex]@@[group]
参数 group 默认为 0。
例子:
@match:http.*?jpg
@match:url\("?(.*?jpg)@@1
替换 @replace: 规则
可以使用 @replace: 规则来正则匹配内容并进行替换。
格式如下:
@replace:[regexp/substr]
@replace:[regexp/substr]@@[replacement]
提示
如果不指定 replacement 参数,会将匹配到的内容删除。
例子:
@replace:</?em>
@replace:(?=\d+)@@播放量
