jsoup学习(二)
上一篇爬去的静态数据,这一篇爬去动态的ajax请求
比如有这么一个需求,经常都会在网站上看到你是第几个访问者,我们能否爬去到这样的动态数据呢?
其实这样的数据都是封装的ajax中的,比如
一、准备
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.4</version>
</dependency>
二、代码:
package jsoup;
import java.io.IOException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public class jsoupTest01 {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper=new ObjectMapper();
String url="http://lib.swpu.edu.cn/index_counter.html";
Response response=Jsoup.connect(url).ignoreContentType(true).execute();
String resultJSON=response.body();
JsonNode jn=objectMapper.readTree(resultJSON);
System.out.println("今日访问人数:"+jn.get("day").toString());
System.out.println("本月访问人数:"+jn.get("month").toString());
System.out.println("年度访问人数:"+jn.get("year").toString());
System.out.println("总共访问人数:"+jn.get("all").toString());
}
}
运行结果:
今日访问人数:2551
本月访问人数:44984
年度访问人数:296512
总共访问人数:12891714
虽然数据不同,说明在运行的过程中,又有人进行了访问