Java 爬虫服务器被屏蔽,不要慌,咱们换一台服务器

2022-10-14,,,,

这是 java 爬虫系列博文的第四篇,在上一篇 java 爬虫遇上数据异步加载,试试这两种办法! 中,我们从内置浏览器内核和反向解析法两个角度简单的聊了聊关于处理数据异步加载问题。在这篇文章中,我们简单的来聊一聊爬虫时,资源网站根据用户访问行为屏蔽掉爬虫程序及其对应的解决办法。

屏蔽爬虫程序是资源网站的一种保护措施,最常用的反爬虫策略应该是基于用户的访问行为。比如限制每台服务器在一定的时间内只能访问 x 次,超过该次数就认为这是爬虫程序进行的访问,基于用户访问行为判断是否是爬虫程序也不止是根据访问次数,还会根据每次请求的user agent 请求头、每次访问的间隔时间等。总的来说是由多个因数决定的,其中以访问次数为主。

反爬虫是每个资源网站自保的措施,旨在保护资源不被爬虫程序占用。例如我们前面使用到的豆瓣网,它会根据用户访问行为来屏蔽掉爬虫程序,每个 ip 在每分钟访问次数达到一定次数后,后面一段时间内的请求返回直接返回 403 错误,以为着你没有权限访问该页面。所以我们今天再次拿豆瓣网为例,我们用程序模拟出这个现象,下面是我编写的一个采集豆瓣电影的程序

/**
 * 采集豆瓣电影
 */
public class crawlermovie {

    public static void main(string[] args) {
        try {
            crawlermovie crawlermovie = new crawlermovie();
            // 豆瓣电影链接
            list<string> movies = crawlermovie.movielist();
            //创建10个线程的线程池
            executorservice exec = executors.newfixedthreadpool(10);
            for (string url : movies) {
                //执行线程
                exec.execute(new crawlmoviethread(url));
            }
            //线程关闭
            exec.shutdown();
        } catch (exception e) {
            e.printstacktrace();
        }
    }

    /**
     * 豆瓣电影列表链接
     * 采用反向解析法
     *
     * @return
     */
    public list<string> movielist() throws exception {
        // 获取100条电影链接
        string url = "https://movie.douban.com/j/search_subjects?type=movie&tag=热门&sort=recommend&page_limit=200&page_start=0";
        closeablehttpclient client = httpclients.createdefault();
        list<string> movies = new arraylist<>(100);
        try {
            httpget httpget = new httpget(url);
            closeablehttpresponse response = client.execute(httpget);
            system.out.println("获取豆瓣电影列表,返回验证码:" + response.getstatusline().getstatuscode());
            if (response.getstatusline().getstatuscode() == 200) {
                httpentity entity = response.getentity();
                string body = entityutils.tostring(entity, "utf-8");
                // 将请求结果格式化成json
                jsonobject jsonobject = json.parseobject(body);
                jsonarray data = jsonobject.getjsonarray("subjects");
                for (int i = 0; i < data.size(); i++) {
                    jsonobject movie = data.getjsonobject(i);
                    movies.add(movie.getstring("url"));
                }
            }
            response.close();
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            client.close();
        }
        return movies;
    }
}
/**
 * 采集豆瓣电影线程
 */
class crawlmoviethread extends thread {
    // 待采集链接
    string url;

    public crawlmoviethread(string url) {
        this.url = url;
    }
    public void run() {
        try {
            connection connection = jsoup.connect(url)
                    .method(connection.method.get)
                    .timeout(50000);
            connection.response response = connection.execute();
            system.out.println("采集豆瓣电影,返回状态码:" + response.statuscode());
        } catch (exception e) {
            system.out.println("采集豆瓣电影,采集出异常:" + e.getmessage());
        }
    }
}

这段程序的逻辑比较简单,先采集到豆瓣热门电影,这里使用直接访问 ajax 获取豆瓣热门电影的链接,然后解析出电影的详情页链接,多线程访问详情页链接,因为只有在多线程的情况下才能达到豆瓣的访问要求。豆瓣热门电影页面如下:

多次运行上面的程序,你最后会得到下图的结果

从上图中我们可以看出,httpclient 访问返回的状态码为 403 ,说明我们已经没有权限访问该页面了,也就是说豆瓣网已经认为我们是爬虫程序啦,拒接掉了我们的访问请求。我们来分析一下我们现在的访问架构,因为我们是直接访问豆瓣网的,所以此时的访问架构如下图所示:

我们想要突破这层限制的话,我们就不能直接访问豆瓣网的服务器,我们需要拉入第三方,让别人代替我们去访问,我们每次访问都找不同的人,这样就不会被限制了,这个也就是所谓的 ip代理。 此时的访问架构就变成了下面这张图:

我们使用的 ip代理,我们就需要有 ip代理池,接下来我们就来聊一聊 ip 代理池

ip 代理池

代理服务器有很多厂商在做这一块,具体的我就不说了,自己百度 ip 代理可以搜出一大堆,这些 ip代理商都有提供收费和免费的代理 ip,收费的代理 ip可用性高,速度快,在线上环境如果需要使用代理的话,建议使用收费的代理 ip。如果只是自己研究的话,我们就可以去采集这些厂商的免费公开代理 ip,这些 ip 的性能和可用性都比较差,但是不影响我们使用。

因为我们是 demo 项目,所以我们就自己搭建 ip代理池。我们该怎么设计一个 ip代理池呢?下图是我画的简单 ip代理池架构图

从上面的架构图中,可以看出一个 ip 代理系统会涉及到 4 个模块,分别为 ip 采集模块、 ip 存储模块、ip 检测模块和 api 接口模块。

  • ip 采集模块

    负责从各大 ip代理厂商采集代理 ip,采集的网站越多,代理 ip 的可用性就越高

  • ip 存储模块

    存储采集回来的代理 ip,比较常用的是 redis 这样的高性能的数据库,在存储方面我们需要存储两种数据,一种是检测可用的代理 ip,另一种是采集回来还未检测的代理 ip。

  • ip 检测模块

    检测采集回来的 ip 是否可用,这样能够让我们提供的 ip 可用性变高,我们先过滤掉不可用的 ip。

  • api 接口模块

    以接口的形式对外提供可用代理 ip

上面就是关于 ip代理池的相关设计,对于这些我们只需要简单了解一下就行了,因为现在基本上不需要我们去编写 ip代理池服务啦,在 github 上已经有大量优秀的开源项目,没必要重复造轮子啦。我为大家选取了在 github 上有 8k star 的开源 ip代理池项目 proxy_pool ,我们将使用它作为我们 ip 代理池。关于 proxy_pool 请访问:https://github.com/jhao104/proxy_pool

部署 proxy_pool

proxy_pool 是用 python 语言写的,不过这也没什么关系,因为现在都可以容器化部署,使用容器化部署可以屏蔽掉一些环境的安装,只需要运行镜像就可以运行服务了,并不需要知道它里面的具体实现,所以这个项目不懂 python 的 java 程序员也是可以使用的。proxy_pool 使用的是 redis 来存储采集的 ip,所以在启动 proxy_pool 前,你需要先启动 redis 服务。下面是 proxy_pool docker启动步骤。

  • 拉取镜像

    docker pull jhao104/proxy_pool

  • 运行镜像

    docker run --env db_type=redis --env db_host=127.0.0.1 --env db_port=6379 --env db_password=pwd_str -p 5010:5010 jhao104/proxy_pool

    运行镜像后,我们等待一段时间,因为第一次启动采集数据和处理数据需要一段时间。等待之后访问 http://{your_host}:5010/get_all/,如果你得到下图所示的结果,说明 proxy_pool 项目你已经部署成功。

使用 ip 代理

搭建好 ip代理池之后,我们就可以使用代理 ip 来采集豆瓣电影啦,我们已经知道了除了 ip 之外,user agent 请求头也会是豆瓣网判断访问是否是爬虫程序的一个因素,所以我们也对 user agent 请求头进行伪造,我们每次访问使用不同的 user agent 请求头。

我们为豆瓣电影采集程序引入 ip代理和 随机 user agent 请求头,具体代码如下:

public class crawlermovieproxy {

    /**
     * 常用 user agent 列表
     */
    static list<string> user_agent = new arraylist<string>(10) {
        {
            add("mozilla/5.0 (linux; android 4.1.1; nexus 7 build/jro03d) applewebkit/535.19 (khtml, like gecko) chrome/18.0.1025.166 safari/535.19");
            add("mozilla/5.0 (linux; u; android 4.0.4; en-gb; gt-i9300 build/imm76d) applewebkit/534.30 (khtml, like gecko) version/4.0 mobile safari/534.30");
            add("mozilla/5.0 (linux; u; android 2.2; en-gb; gt-p1000 build/froyo) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1");
            add("mozilla/5.0 (windows nt 6.2; wow64; rv:21.0) gecko/20100101 firefox/21.0");
            add("mozilla/5.0 (android; mobile; rv:14.0) gecko/14.0 firefox/14.0");
            add("mozilla/5.0 (windows nt 6.2; wow64) applewebkit/537.36 (khtml, like gecko) chrome/27.0.1453.94 safari/537.36");
            add("mozilla/5.0 (linux; android 4.0.4; galaxy nexus build/imm76b) applewebkit/535.19 (khtml, like gecko) chrome/18.0.1025.133 mobile safari/535.19");
            add("mozilla/5.0 (ipad; cpu os 5_0 like mac os x) applewebkit/534.46 (khtml, like gecko) version/5.1 mobile/9a334 safari/7534.48.3");
            add("mozilla/5.0 (ipod; u; cpu like mac os x; en) applewebkit/420.1 (khtml, like gecko) version/3.0 mobile/3a101a safari/419.3");
        }
    };

    /**
     * 随机获取 user agent
     *
     * @return
     */
    public string randomuseragent() {
        random random = new random();
        int num = random.nextint(user_agent.size());
        return user_agent.get(num);
    }

    /**
     * 设置代理ip池
     *
     * @param queue 队列
     * @throws ioexception
     */
    public void proxyippool(linkedblockingqueue<string> queue) throws ioexception {


        // 每次能随机获取一个代理ip
        string proxyurl = "http://192.168.99.100:5010/get_all/";

        closeablehttpclient httpclient = httpclients.createdefault();

        httpget httpget = new httpget(proxyurl);
        closeablehttpresponse response = httpclient.execute(httpget);
        if (response.getstatusline().getstatuscode() == 200) {
            httpentity entity = response.getentity();
            string body = entityutils.tostring(entity, "utf-8");

            jsonarray jsonarray = json.parsearray(body);
            int size = math.min(100, jsonarray.size());
            for (int i = 0; i < size; i++) {
                // 将请求结果格式化成json
                jsonobject data = jsonarray.getjsonobject(i);
                string proxy = data.getstring("proxy");
                queue.add(proxy);
            }
        }
        response.close();
        httpclient.close();
        return;
    }


    /**
     * 随机获取一个代理ip
     *
     * @return
     * @throws ioexception
     */
    public string randomproxyip() throws ioexception {

        // 每次能随机获取一个代理ip
        string proxyurl = "http://192.168.99.100:5010/get/";

        string proxy = "";

        closeablehttpclient httpclient = httpclients.createdefault();

        httpget httpget = new httpget(proxyurl);
        closeablehttpresponse response = httpclient.execute(httpget);
        if (response.getstatusline().getstatuscode() == 200) {
            httpentity entity = response.getentity();
            string body = entityutils.tostring(entity, "utf-8");
            // 将请求结果格式化成json
            jsonobject data = json.parseobject(body);
            proxy = data.getstring("proxy");
        }
        return proxy;
    }

    /**
     * 豆瓣电影链接列表
     *
     * @return
     */
    public list<string> movielist(linkedblockingqueue<string> queue) {
        // 获取60条电影链接
        string url = "https://movie.douban.com/j/search_subjects?type=movie&tag=热门&sort=recommend&page_limit=40&page_start=0";
        list<string> movies = new arraylist<>(40);
        try {
            closeablehttpclient client = httpclients.createdefault();
            httpget httpget = new httpget(url);
            // 设置 ip 代理
            httphost proxy = null;
            // 随机获取一个代理ip
            string proxy_ip = randomproxyip();
            if (stringutils.isnotblank(proxy_ip)) {
                string[] proxylist = proxy_ip.split(":");
                system.out.println(proxylist[0]);
                proxy = new httphost(proxylist[0], integer.parseint(proxylist[1]));
            }
            // 随机获取一个请求头
            httpget.setheader("user-agent", randomuseragent());
            requestconfig requestconfig = requestconfig.custom()
                    .setproxy(proxy)
                    .setconnecttimeout(10000)
                    .setsockettimeout(10000)
                    .setconnectionrequesttimeout(3000)
                    .build();
            httpget.setconfig(requestconfig);
            closeablehttpresponse response = client.execute(httpget);
            system.out.println("获取豆瓣电影列表,返回验证码:" + response.getstatusline().getstatuscode());
            if (response.getstatusline().getstatuscode() == 200) {
                httpentity entity = response.getentity();
                string body = entityutils.tostring(entity, "utf-8");
                // 将请求结果格式化成json
                jsonobject jsonobject = json.parseobject(body);
                jsonarray data = jsonobject.getjsonarray("subjects");
                for (int i = 0; i < data.size(); i++) {
                    jsonobject movie = data.getjsonobject(i);
                    movies.add(movie.getstring("url"));
                }
            }
            response.close();
        } catch (exception e) {
            e.printstacktrace();
        } finally {

        }
        return movies;
    }


    public static void main(string[] args) {
        // 存放代理ip的队列
        linkedblockingqueue<string> queue = new linkedblockingqueue(100);

        try {
            crawlermovieproxy crawlerproxy = new crawlermovieproxy();
            // 初始化ip代理队列
            crawlerproxy.proxyippool(queue);
            // 获取豆瓣电影列表
            list<string> movies = crawlerproxy.movielist(queue);

            //创建固定大小的线程池
            executorservice exec = executors.newfixedthreadpool(5);
            for (string url : movies) {
                //执行线程
                exec.execute(new crawlmovieproxythread(url, queue, crawlerproxy));
            }
            //线程关闭
            exec.shutdown();
        } catch (exception e) {
            e.printstacktrace();
        }
    }

}

/**
 * 采集豆瓣电影线程
 */
class crawlmovieproxythread extends thread {
    // 待采集链接
    string url;
    // 代理ip队列
    linkedblockingqueue<string> queue;
    // 代理类
    crawlermovieproxy crawlerproxy;

    public crawlmovieproxythread(string url, linkedblockingqueue<string> queue, crawlermovieproxy crawlerproxy) {
        this.url = url;
        this.queue = queue;
        this.crawlerproxy = crawlerproxy;
    }

    public void run() {
        string proxy;
        string[] proxys = new string[2];
        try {
            connection connection = jsoup.connect(url)
                    .method(connection.method.get)
                    .timeout(50000);

            // 如果代理ip队列为空,则重新获取ip代理
            if (queue.size() == 0) crawlerproxy.proxyippool(queue);
            // 从队列中获取代理ip
            proxy = queue.poll();
            // 解析代理ip
            proxys = proxy.split(":");
            // 设置代理ip
            connection.proxy(proxys[0], integer.parseint(proxys[1]));
            // 设置 user agent
            connection.header("user-agent", crawlerproxy.randomuseragent());
            connection.response response = connection.execute();
            system.out.println("采集豆瓣电影,返回状态码:" + response.statuscode() + " ,请求ip:" + proxys[0]);
        } catch (exception e) {
            system.out.println("采集豆瓣电影,采集出异常:" + e.getmessage() + " ,请求ip:" + proxys[0]);
        }
    }
}

运行修改后的采集程序,可能需要多次运行,因为你的代理 ip 不一定每次都有效。代理 ip 有效的话,你将得到如下结果

结果中我们可以看出,40 次的电影详情页访问,有大量的代理 ip 是无效的,只有一小部分的代理 ip 有效。结果直接证明了免费的代理 ip 可用性不高,所以如果线上需要使用代理 ip 的话,最好使用收费的代理 ip。尽管我们自己搭建的 ip代理池可用性不是太高,但是我们设置的 ip 代理访问豆瓣电影已经成功了,使用 ip 代理成功绕过了豆瓣网的限制。

关于爬虫服务器被屏蔽,原因有很多,我们这篇文章主要介绍的是通过 设置 ip 代理和伪造 user agent 请求头来绕过豆瓣网的访问限制。如何让我们的程序不被资源网站视为爬虫程序呢?需要做好以下三点:

  • 伪造 user agent 请求头
  • 使用 ip 代理
  • 不固定的采集间隔时间

希望这篇文章对你有所帮助,下一篇是关于多线程爬虫的探索。如果你对爬虫感兴趣,不妨关注一波,相互学习,相互进步

文章不足之处,望大家多多指点,共同学习,共同进步

最后

打个小广告,欢迎扫码关注微信公众号:「平头哥的技术博文」,一起进步吧。

《Java 爬虫服务器被屏蔽,不要慌,咱们换一台服务器.doc》

下载本文的Word格式文档,以方便收藏与打印。