一、爬取URL链接是get请求
比如:
![](https://upload-images.jianshu.io/upload_images/23088632-6f189c1092145821.png?imageMogr2/auto-orient/strip|imageView2/2/w/708/format/webp)
![](https://upload-images.jianshu.io/upload_images/23088632-fd1260fc20eceb02.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
这是taptap游戏厂商排行榜网,现在就可以使用OkHttpClient根据这个url爬取到json的字符串
代码如下:
![](https://upload-images.jianshu.io/upload_images/23088632-6653a9c2013150b2.png?imageMogr2/auto-orient/strip|imageView2/2/w/875/format/webp)
接下来就简单了,使用JSON转化将result转化为实体类,
比如使用import com.alibaba.fastjson.JSON;中的JSON:
![](https://upload-images.jianshu.io/upload_images/23088632-45a9698142ba69dc.png?imageMogr2/auto-orient/strip|imageView2/2/w/938/format/webp)
因为字符串html获取到的数据是前端html拼接起来的字符串,所以剩下的就可以用Jsoup解析想要的数据了
二、爬取URL链接是Post请求
比如:
![](https://upload-images.jianshu.io/upload_images/23088632-6f7d4ca396f04161.png?imageMogr2/auto-orient/strip|imageView2/2/w/609/format/webp)
这时候就得知道post请求的Form Data了:
![](https://upload-images.jianshu.io/upload_images/23088632-d57a36d3d1b52ebb.png?imageMogr2/auto-orient/strip|imageView2/2/w/597/format/webp)
这是新榜排名列表微博日榜个人认证的url;使用OkHttpClient的时候就得加上这些条件,如:
![](https://upload-images.jianshu.io/upload_images/23088632-b81d23d73e72d0e7.png?imageMogr2/auto-orient/strip|imageView2/2/w/958/format/webp)
接下来就简单了,使用JSon解析一下就可以得到自己想要的类或对象数据了。
三、使用到的pom文件(jar包)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>