如果爬取数据的量很大,而且使用代理Ip封号很频繁,那么就应该考虑动态代理ip,如果这个Ip访问不了,就换下一个ip继续爬取数据。
用于动态ip代理的工具类HttpUtils,代码如下:
![](https://upload-images.jianshu.io/upload_images/23088632-a31468a7d5630c3f.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
上图中的“D:\ips.txt”如下图所示,都是代理Ip,全部集合在一起,方便换取。
![](https://upload-images.jianshu.io/upload_images/23088632-1fcc23a4f205d47b.png?imageMogr2/auto-orient/strip|imageView2/2/w/592/format/webp)
用于获取document对象的工具类JsoupUtils,代码如下:
![](https://upload-images.jianshu.io/upload_images/23088632-1887073e3072244f.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
实际项目需要用动态代理IP,就可以调用上面两个工具类,代码如下图所示;
![](https://upload-images.jianshu.io/upload_images/23088632-ef296aabd135e8dd.png?imageMogr2/auto-orient/strip|imageView2/2/w/820/format/webp)
下图是代理Ip不能访问的效果图
![](https://upload-images.jianshu.io/upload_images/23088632-a4b74c3a810a530b.png?imageMogr2/auto-orient/strip|imageView2/2/w/986/format/webp)
下面是代理ip访问的效果图,根据我的项目代码,是可以爬取数据插入数据库的,如下图所示:
![](https://upload-images.jianshu.io/upload_images/23088632-a7e6a0cd76c4bdc3.png?imageMogr2/auto-orient/strip|imageView2/2/w/1092/format/webp)