日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WebMagic学习-解析json

發(fā)布時(shí)間:2023/12/20 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WebMagic学习-解析json 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

這篇文章要解決什么

當(dāng)頁面使用前端ajax方式渲染的頁面數(shù)據(jù)時(shí),頁面會(huì)使用js請求ajaxUrl獲取json格式數(shù)據(jù)時(shí),然后再用js把數(shù)據(jù)解析并渲染到頁面的指定位置上。

?

錯(cuò)誤寫法

當(dāng)爬蟲要住區(qū)ajaxUrl返回的json格式數(shù)據(jù)時(shí),我當(dāng)時(shí)是這樣寫的:

// 聲明:下面這種方法是錯(cuò)誤的。是我沒有看官方demo的時(shí)候憑感覺寫出的解析json數(shù)據(jù)的代碼。 JsonPathSelector json = new JsonPathSelector(page.getRawText()); List<String> name = json.selectList("$.data.itemList[*].brand.name"); List<String> uri = json.selectList("$.data.itemList[*].brand.uri");

看了官方us.codecraft.webmagic.selector.JsonPathSelectorTest,才知道原來參數(shù)寫錯(cuò)了。

?

正確寫法

JsonPathSelector(String jsonPathStr)這個(gè)構(gòu)造函數(shù)的參數(shù)是jsonPathStr,也就是提取規(guī)則的字符串

String select(String text)方法List<String> selectList(String text)方法,參數(shù)都是text,也就是json的字符串。

?

Demo

package us.codecraft.webmagic.selector;import org.junit.Test;import java.util.List;import static org.assertj.core.api.Assertions.assertThat;/*** @author code4crafter@gmai.com <br>*/ public class JsonPathSelectorTest {private String text = "{ \"store\": {\n" +" \"book\": [ \n" +" { \"category\": \"reference\",\n" +" \"author\": \"Nigel Rees\",\n" +" \"title\": \"Sayings of the Century\",\n" +" \"price\": 8.95\n" +" },\n" +" { \"category\": \"fiction\",\n" +" \"author\": \"Evelyn Waugh\",\n" +" \"title\": \"Sword of Honour\",\n" +" \"price\": 12.99,\n" +" \"isbn\": \"0-553-21311-3\"\n" +" }\n" +" ],\n" +" \"bicycle\": {\n" +" \"color\": \"red\",\n" +" \"price\": 19.95\n" +" }\n" +" }\n" +"}";@Testpublic void testJsonPath() {System.out.println("需要解析的json:"+text);JsonPathSelector jsonPathSelector = new JsonPathSelector("$.store.book[*].author");String select = jsonPathSelector.select(text);List<String> list = jsonPathSelector.selectList(text);assertThat(select).isEqualTo("Nigel Rees");assertThat(list).contains("Nigel Rees","Evelyn Waugh");jsonPathSelector = new JsonPathSelector("$.store.book[?(@.category == 'reference')]");list = jsonPathSelector.selectList(text);select = jsonPathSelector.select(text);System.out.println("select方法的結(jié)果:\t"+select);System.out.println("selectList方法的結(jié)果:\t"+list);assertThat(select).isEqualTo("{\"author\":\"Nigel Rees\",\"price\":8.95,\"category\":\"reference\",\"title\":\"Sayings of the Century\"}");assertThat(list).contains("{\"author\":\"Nigel Rees\",\"price\":8.95,\"category\":\"reference\",\"title\":\"Sayings of the Century\"}");} }

?

個(gè)人見解

我覺得這個(gè)實(shí)現(xiàn)不太好。在一個(gè)page中,jsonStr是一樣的,而提取規(guī)則不同。如果每次都new 一個(gè)新的JsonPathSelector作為提取規(guī)則,那要?jiǎng)?chuàng)建多少對象啊。而且和下面這種實(shí)現(xiàn)比較來說,提取規(guī)則開發(fā)方式不同:

String brand_price = html.xpath("//span[@id=\"item-sellprice\"]/text()").toString(); String brand_img = html.xpath("//img[@id=\"brand-img\"]/@src").toString(); String brand_describe = html.xpath("//p[@id=\"brand-describe\"]/text()").toString(); String location_text = html.xpath("//span[@id=\"location-text\"]/text()").toString();

估計(jì)不是我自己出現(xiàn)這種問題吧,所以就記錄一下。嘿嘿。

轉(zhuǎn)載于:https://my.oschina.net/anxiaole/blog/782026

總結(jié)

以上是生活随笔為你收集整理的WebMagic学习-解析json的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。