什么是JSON采集?

2020-03-20 663 19 编辑:蜀戎seo 来源:蜀戎网

json是一种轻量级的数据交换格式,由于一些优秀的特性比如比 XML 更小、更快,更易解析和阅读,并有效地提升网络传输效率,而得到广泛使用。


Json采集的优点:

1、无需加载图片视频等信息,采集速度更快

2、部分网站防采集限制减少,采集更加顺畅

如何采集json数据,请看json教程:

使用JsonPath表达式从Json字符串中提取数据,JsonPath和XPath有点相似,但写法却是不一样的,这里我们对比一下XPath来学习JsonPath的用法。    先上一段Json字符:

{ "store": {

    "book":  

      { "category": "reference",

        "author": "Nigel Rees",

        "title": "Sayings of the Century",

        "price": 8.95

      },

      { "category": "fiction",

        "author": "Evelyn Waugh",

        "title": "Sword of Honour",

        "price": 12.99

      },

      { "category": "fiction",

        "author": "Herman Melville",

        "title": "Moby Dick",

        "isbn": "0-553-21311-3",

        "price": 8.99

      },

      { "category": "fiction",

        "author": "J. R. R. Tolkien",

        "title": "The Lord of the Rings",

        "isbn": "0-395-19395-8",

        "price": 22.99

      }

    ,

    "bicycle": {

      "color": "red",

      "price": 19.95

    }

  }

}

比如我们要提取第一本书(book)的标题(title),如果我们把它当做是Xml的话,可以这样写

XPath:/store/book1/title

而使用JsonPath则是这样写

JsonPath:$.store.book0.title

或者也可以这样写 $'store''book'0'title'


JsonPath中的$相当于Xpath中的首字符"/",代表“根成员对象”,因为JSON结构通常是匿名的,不一定有一个“根成员对象”,所以这个$实际上是虚拟出来的,这里我们不用管它,知道他代表根对象就行了。而XPath中间部分的子操作符“/“,在JsonPath中则是用"."来表示,或者用第二种写法,用中括号""来括住节点名称(节点名称要加上单引号)。

下面列个表格对比一下


XPath

JsonPath

说明

/

$

根对象/元素

.

@

当前对象/元素

/

. or

子操作符

..

n/a

父操作符

//

..

递归查找所有级别的子级元素

*

*

通配符

@

查找指定属性,json中无属性的定义



subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.

|

,

Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.

start:end:step

array slice operator borrowed from ES4.


?()

用于过滤

()

脚本表达式,使用底层的脚本引擎。

()

Xpath中的分组

相关知识点: JSON采集
本站文章均为蜀戎网络摘自权威资料,书籍,或网络原创文章,如有版权纠纷或者违规问题,请即刻联系我们删除,未经允许禁止复制转载!感谢...
更多人喜欢

在线
客服

在线客服服务时间:9:00-21:00

客服
热线

13227777380
7*24小时客服服务热线

客服
微信

扫一扫微信咨询
顶部