网络爬虫使用HTTP代理的关键性

172 阅读2分钟

网络爬虫在做爬虫工作的时候可能会遇到ip问题,无论IP限制,还是爬虫行为限制。这个时候就会比较麻烦,不过可以通过更换HTTP代理或者做爬虫反爬优化策略来解决问题。

随着时代的进步,网络的发达,依靠网络工作的人员也越来越多,HTTP代理IP的存在帮助了很多网络工作人员达到了高效率的工作。HTTP代理IP不仅网络工作人员可以常用到,个人也可以使用,来保护隐私。

而且许多工作需要网络完成,如问答推广,网络营销,数据收集等等,但频繁的操作会导致目标网站阻止你的IP地址,这样你就无法访问目标网站。此时,您需要使用代理IP。

网络爬虫对代理IP服务器质量要求比较高,代理服务器的数量跟质量也在不断完善与提升。

采集数据运用代理IP能够突破IP的限制,不仅能加快采集的速度。现在互联网的很多网站都有反爬虫机制,假如识别到正常用户访问就可以正常,假如快速的反复访问,就非常容易被识别限制您的访问行为,因此被封IP。此时代理IP就特别重要了,反爬虫机制只会识别IP地址,运用代理IP就可以轻轻松松更换IP地址,爬虫工作就可以顺利进行了。

网络爬虫采集数据运用代理IP是必需品,网络爬虫运用代理IP一定要用隧道转发的爬虫代理加强版高匿名代理,透明代理及普通匿名代理都会被对方识别,一样会被封IP。

<?php    
    namespace App\Console\Commands;    
    use Illuminate\Console\Command;

    class Test16Proxy extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'test:16proxy';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description';

        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }

        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            $client = new \GuzzleHttp\Client();
            // 要访问的目标页面
            $targetUrl = "http://httpbin.org/ip";

            // 代理服务器(产品官网 www.16yun.cn)
            define("PROXY_SERVER", "t.16yun.cn:31111");

            // 代理身份信息
            define("PROXY_USER", "username");
            define("PROXY_PASS", "password");

            $proxyAuth = base64_encode(PROXY_USER . ":" . PROXY_PASS);

            $options = [
                "proxy"  => PROXY_SERVER,
                "headers" => [
                    "Proxy-Authorization" => "Basic " . $proxyAuth
                ]
            ];
            //print_r($options);
            $result = $client->request('GET', $targetUrl, $options);
            var_dump($result->getBody()->getContents());
        }
    }
?>