LoginSignup
3
2

More than 5 years have passed since last update.

サルでも分かるPHP goutteでスクレイピング

Posted at

curl -sS https://getcomposer.org/installer | php

↑composer.phar ダウンロード

php composer.phar require fabpot/goutte:~2.0

↑goutteインストール。vendorディレクトリが生成される。composer.jsonに下記が記述される。

{
"require": {
"fabpot/goutte": "~2.0"
}
}

下記の通りphpファイルを作成。
http://xxx.com/1 から http://xxx.com/100 まで100ページに渡り、

に囲まれた部分を拾ってくる。
アウトプットはターミナルに表示されるが、php filename.php > text.txt などで保存可。
<?php
require_once ‘./vendor/autoload.php’; //goutte呼ぶ

$client = new Goutte\Client();
for($i=1;$i<100;$i++){ //100ページまで読む
$crawler = $client->request(‘GET’, ‘http://xxx.com/’.$i); //読みたいサイト

// 抽出
$target/*Selector*/ = ‘div.classname’; //拾う場所。この例だと<div class="classname">ここを拾ってくる</div>
$crawler->filter($target)->each(function ($node) {
$word = $node->text();
echo $word . "\n"; //ターミナルに表示
});
}

色々と応用できますね(邪

3
2
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
3
2