add more sample for jsonpath #42
parent
59ad4cad27
commit
18a3af4a0a
|
@ -3,11 +3,15 @@ package us.codecraft.webmagic.example;
|
||||||
import us.codecraft.webmagic.Site;
|
import us.codecraft.webmagic.Site;
|
||||||
import us.codecraft.webmagic.model.OOSpider;
|
import us.codecraft.webmagic.model.OOSpider;
|
||||||
import us.codecraft.webmagic.model.annotation.ExtractBy;
|
import us.codecraft.webmagic.model.annotation.ExtractBy;
|
||||||
|
import us.codecraft.webmagic.utils.Experimental;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author code4crafter@gmail.com
|
* @author code4crafter@gmail.com
|
||||||
* @since 0.4.1
|
* @since 0.4.1
|
||||||
*/
|
*/
|
||||||
|
@Experimental
|
||||||
public class AppStore {
|
public class AppStore {
|
||||||
|
|
||||||
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..trackName")
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..trackName")
|
||||||
|
@ -16,9 +20,17 @@ public class AppStore {
|
||||||
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..description")
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..description")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..userRatingCount")
|
||||||
|
private int userRatingCount;
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..screenshotUrls",multi = true)
|
||||||
|
private List<String> screenshotUrls;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AppStore appStore = OOSpider.create(Site.me(), AppStore.class).<AppStore>get("http://itunes.apple.com/lookup?id=653350791&country=cn&entity=software");
|
AppStore appStore = OOSpider.create(Site.me(), AppStore.class).<AppStore>get("http://itunes.apple.com/lookup?id=653350791&country=cn&entity=software");
|
||||||
System.out.println(appStore.trackName);
|
System.out.println(appStore.trackName);
|
||||||
System.out.println(appStore.description);
|
System.out.println(appStore.description);
|
||||||
|
System.out.println(appStore.userRatingCount);
|
||||||
|
System.out.println(appStore.screenshotUrls);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
package us.codecraft.webmagic.example;
|
||||||
|
|
||||||
|
import us.codecraft.webmagic.Site;
|
||||||
|
import us.codecraft.webmagic.model.ConsolePageModelPipeline;
|
||||||
|
import us.codecraft.webmagic.model.HasKey;
|
||||||
|
import us.codecraft.webmagic.model.OOSpider;
|
||||||
|
import us.codecraft.webmagic.model.annotation.ExtractBy;
|
||||||
|
import us.codecraft.webmagic.model.annotation.ExtractByUrl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author code4crafter@gmail.com <br>
|
||||||
|
* @since 0.4.1
|
||||||
|
*/
|
||||||
|
public class GithubRepoApi implements HasKey {
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.name")
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..owner.login")
|
||||||
|
private String author;
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.language",multi = true)
|
||||||
|
private List<String> language;
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.stargazers_count")
|
||||||
|
private int star;
|
||||||
|
|
||||||
|
@ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.forks_count")
|
||||||
|
private int fork;
|
||||||
|
|
||||||
|
@ExtractByUrl
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
OOSpider.create(Site.me().setSleepTime(100)
|
||||||
|
, new ConsolePageModelPipeline(), GithubRepoApi.class)
|
||||||
|
.addUrl("https://api.github.com/repos/code4craft/webmagic").run();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String key() {
|
||||||
|
return author + ":" + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getStar() {
|
||||||
|
return star;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFork() {
|
||||||
|
return fork;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package us.codecraft.webmagic.selector;
|
package us.codecraft.webmagic.selector;
|
||||||
|
|
||||||
import com.jayway.jsonpath.JsonPath;
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
import us.codecraft.webmagic.utils.Experimental;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -12,6 +13,7 @@ import java.util.List;
|
||||||
* @author code4crafter@gmail.com <br>
|
* @author code4crafter@gmail.com <br>
|
||||||
* @since 0.2.1
|
* @since 0.2.1
|
||||||
*/
|
*/
|
||||||
|
@Experimental
|
||||||
public class JsonPathSelector implements Selector {
|
public class JsonPathSelector implements Selector {
|
||||||
|
|
||||||
private String jsonPathStr;
|
private String jsonPathStr;
|
||||||
|
|
Loading…
Reference in New Issue