update AngularJSProcessor
parent
03c251237b
commit
84b897f83b
@ -1,21 +1,43 @@
|
||||
package us.codecraft.webmagic.samples;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import us.codecraft.webmagic.Page;
|
||||
import us.codecraft.webmagic.Site;
|
||||
import us.codecraft.webmagic.processor.PageProcessor;
|
||||
import us.codecraft.webmagic.selector.JsonPathSelector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yihua.huang@dianping.com
|
||||
* @author code4crafter@gmail.com
|
||||
* @since 0.5.0
|
||||
*/
|
||||
public class AngularJSProcessor implements PageProcessor{
|
||||
public class AngularJSProcessor implements PageProcessor {
|
||||
|
||||
private Site site = Site.me();
|
||||
|
||||
private static final String ARITICALE_URL = "http://angularjs\\.cn/api/article/\\w+";
|
||||
|
||||
private static final String LIST_URL = "http://angularjs\\.cn/api/article/latest.*";
|
||||
|
||||
@Override
|
||||
public void process(Page page) {
|
||||
if (page.getUrl().regex(LIST_URL).match()) {
|
||||
List<String> ids = new JsonPathSelector("$.data._id").selectList(page.getRawText());
|
||||
if (CollectionUtils.isNotEmpty(ids)) {
|
||||
for (String id : ids) {
|
||||
page.addTargetRequest("http://angularjs\\.cn/api/article/" + id);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
page.putField("title", new JsonPathSelector("$.title").select(page.getRawText()));
|
||||
page.putField("content", new JsonPathSelector("$.content").select(page.getRawText()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Site getSite() {
|
||||
return null;
|
||||
return site;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue