LoginSignup
44
49

More than 5 years have passed since last update.

プロキシ経由でスクレイピング★

Posted at

こんばんは、なめこです
今回はGoutteを使って、プロキシ経由でスクレイピングをしてみたときのメモです。

Goutte
https://github.com/fabpot/goutte

まずはプロキシのIPアドレスはどこから取得するか?ですが
http://lab.magicvox.net/proxy/
ここからげっちゅします。
もちろんスクレイピングで。

php|Proxy.php
<?php
namespace Scraping;

class Proxy {

    private $client;
    private $url;

    public function __construct($client, $url)
    {
        $this->client = $client;
        $this->url = $url;
    }

    public function getProxyIpList()
    {
        static $errorCount = 0;

        try {
            $crawler = $this->client->request('GET', $this->url);
            $dom = $crawler->filter('table tr');

            $ips = array();
            $dom->each(function ($node) use (&$ips) {
                if ($node->filter('td')->count()) {
                    $host = $node->filter('td.host')->text();
                    $port = $node->filter('td.port')->text();
                    $ip = $node->text();
                    $ips[] = $host . ':' . $port;
                }
            });
        } catch (\Exception $e) {
            $errorCount++;
            if ($errorCount > 10) {
                return null;
            }
            return $this->getProxyIpList();
        }

        return $ips;
    }
}

コンストラクタで必要なものは、外部から注入してください。
このクラスのgetProxyIpListで


[0] => 202.106.16.36:3128
[1] => 202.56.231.117:80
みたいに配列で取得できるよ!(念のためリトライ処理入れてます)
php|Proxy.php
抜粋.....
    public function __construct($client, $url)
    {
        $this->client = $client;
        $this->url = $url;
    }

ここのclientだけど、


// ユーザーエージェントの取得
$useragent = HttpHeaderUtil::getUserAgent();
$client = new Client(['HTTP_USER_AGENT'=>$useragent]);

みたいにGoutteのClientクラスを作って、こいつを渡してあげよう。
HttpHeaderUtil::getUserAgent()は自前で用意したUserAgentのリストファイルから適当に選んで返却してくれる
Utilだよ!必要なら作ってね!
プロキシIPとってくるときは、自サーバのIPなので、まだプロキシ経由じゃないね。

そしてプロキシ用のClientの作成・・・

php|ProxyClient.php
<?php
namespace Scraping;

use GuzzleHttp\Client as GuzzleClient;
use Goutte\Client;
use Scraping\HttpHeaderUtil;

class ProxyClient {
    private $proxyIp;
    public function __construct($proxyIp)
    {
        $this->proxyIp = $proxyIp;
    }
    public function getClientInstacne()
    {
        $gclient = new GuzzleClient([
            'defaults' => [
            'proxy'   => 'tcp://'.$this->proxyIp
            ]
        ]);
        // ユーザーエージェントの取得(自前)
        $useragent = HttpHeaderUtil::getUserAgent();
        $client = new Client(['HTTP_USER_AGENT'=>$useragent]);
        $client = $client->setClient($gclient);
        $client->getClient()->setDefaultOption('config/curl/'.CURLOPT_TIMEOUT,60);
        return $client;
    }
}

コンストラクタの$proxyIpはProxyクラスで取得したIPリストから適当にひとつ選んで渡してあげよう
あとはClient使ってスクレイピングしよう!


ProxyClientのgetClientInstacneで取得したclientを使って。。。。。
$crawler = $client->request('GET', 'http://qiita.com/');
$title = $crawler->filter('title')->text(); //てきとう

Goutteは怖いけど(過去記事参照)、便利だよ!

44
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
49