Commit 03c25123 authored by yihua.huang's avatar yihua.huang

add Json parse support

parent 843e928c
...@@ -65,6 +65,17 @@ ...@@ -65,6 +65,17 @@
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -2,6 +2,7 @@ package us.codecraft.webmagic; ...@@ -2,6 +2,7 @@ package us.codecraft.webmagic;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.selector.Html; import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Json;
import us.codecraft.webmagic.selector.Selectable; import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.webmagic.utils.UrlUtils; import us.codecraft.webmagic.utils.UrlUtils;
...@@ -31,6 +32,8 @@ public class Page { ...@@ -31,6 +32,8 @@ public class Page {
private Html html; private Html html;
private Json json;
private String rawText; private String rawText;
private Selectable url; private Selectable url;
...@@ -72,6 +75,19 @@ public class Page { ...@@ -72,6 +75,19 @@ public class Page {
return html; return html;
} }
/**
* get json content of page
*
* @return json
* @since 0.5.0
*/
public Json getJson() {
if (json == null) {
json = new Json(rawText);
}
return json;
}
/** /**
* @param html * @param html
* @deprecated since 0.4.0 * @deprecated since 0.4.0
......
package us.codecraft.webmagic.selector;
import com.alibaba.fastjson.JSON;
import org.jsoup.parser.TokenQueue;
import java.util.List;
/**
* parse json
* @author code4crafter@gmail.com
* @since 0.5.0
*/
public class Json extends PlainText {
public Json(List<String> strings) {
super(strings);
}
public Json(String text) {
super(text);
}
/**
* remove padding for JSONP
* @param padding
* @return
*/
public Json removePadding(String padding) {
String text = getText();
TokenQueue tokenQueue = new TokenQueue(text);
tokenQueue.consumeWhitespace();
tokenQueue.consume(padding);
tokenQueue.consumeWhitespace();
String chompBalanced = tokenQueue.chompBalanced('(', ')');
return new Json(chompBalanced);
}
public <T> T toObject(Class<T> clazz) {
if (getText() == null) {
return null;
}
return JSON.parseObject(getText(), clazz);
}
public <T> List<T> toList(Class<T> clazz) {
if (getText() == null) {
return null;
}
return JSON.parseArray(getText(), clazz);
}
public String getText() {
if (strings != null && strings.size() > 0) {
return strings.get(0);
}
return null;
}
@Override
public Selectable jsonPath(String jsonPath) {
JsonPathSelector jsonPathSelector = new JsonPathSelector(jsonPath);
return selectList(jsonPathSelector,strings);
}
}
...@@ -22,7 +22,7 @@ public class JsonPathSelector implements Selector { ...@@ -22,7 +22,7 @@ public class JsonPathSelector implements Selector {
public JsonPathSelector(String jsonPathStr) { public JsonPathSelector(String jsonPathStr) {
this.jsonPathStr = jsonPathStr; this.jsonPathStr = jsonPathStr;
this.jsonPath = JsonPath.compile(jsonPathStr); this.jsonPath = JsonPath.compile(this.jsonPathStr);
} }
@Override @Override
......
...@@ -108,6 +108,11 @@ public class PlainText implements Selectable { ...@@ -108,6 +108,11 @@ public class PlainText implements Selectable {
return strings; return strings;
} }
@Override
public Selectable jsonPath(String jsonPath) {
throw new UnsupportedOperationException();
}
@Override @Override
public String get() { public String get() {
if (CollectionUtils.isNotEmpty(all())) { if (CollectionUtils.isNotEmpty(all())) {
......
...@@ -119,4 +119,13 @@ public interface Selectable { ...@@ -119,4 +119,13 @@ public interface Selectable {
* @return multi string result * @return multi string result
*/ */
public List<String> all(); public List<String> all();
/**
* extract by JSON Path expression
*
* @param jsonPath
* @return
*/
public Selectable jsonPath(String jsonPath);
} }
package us.codecraft.webmagic; package us.codecraft.webmagic;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import us.codecraft.webmagic.selector.Html; import us.codecraft.webmagic.selector.Html;
...@@ -14,7 +13,8 @@ public class HtmlTest { ...@@ -14,7 +13,8 @@ public class HtmlTest {
@Test @Test
public void testRegexSelector() { public void testRegexSelector() {
Html selectable = new Html("aaaaaaab"); Html selectable = new Html("aaaaaaab");
Assert.assertEquals("abbabbab", (selectable.regex("(.*)").replace("aa(a)", "$1bb").toString())); // Assert.assertEquals("abbabbab", (selectable.regex("(.*)").replace("aa(a)", "$1bb").toString()));
System.out.println(selectable.regex("(.*)").replace("aa(a)", "$1bb").toString());
} }
......
package us.codecraft.webmagic.selector;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author code4crafter@gmai.com
* @since 0.5.0
*/
public class JsonTest {
private String text = "callback({\"name\":\"json\"})";
@Test
public void testRemovePadding() throws Exception {
String name = new Json(text).removePadding("callback").jsonPath("$.name").get();
assertThat(name).isEqualTo("json");
}
}
...@@ -10,10 +10,6 @@ ...@@ -10,10 +10,6 @@
<artifactId>webmagic-extension</artifactId> <artifactId>webmagic-extension</artifactId>
<dependencies> <dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency> <dependency>
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
...@@ -28,11 +24,6 @@ ...@@ -28,11 +24,6 @@
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package us.codecraft.webmagic.samples;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
/**
* @author yihua.huang@dianping.com
*/
public class AngularJSProcessor implements PageProcessor{
@Override
public void process(Page page) {
}
@Override
public Site getSite() {
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment