Created
November 12, 2014 11:45
-
-
Save momota10s/ac5a332605a55d45803f to your computer and use it in GitHub Desktop.
ゲームアプリのキャラクター情報を収集するコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//外部ファイル(goutte.phar)の呼び出し | |
require_once 'goutte.phar'; | |
use Goutte\Client; | |
//Goutteオブジェクトの生成 | |
$client = new Client(); | |
//配列(URLデータ)の宣言 | |
$row_url = array(); | |
//リクエスト | |
$crawler_url = $client->request('GET','http://cross-summoner.gamerch.com/%E3%83%A6%E3%83%8B%E3%83%83%E3%83%88%E4%B8%80%E8%A6%A7'); | |
//hrefのデータを抽出 | |
for($i=1; $i<=92; $i++){ | |
$crawler_url->filter('#js_async_main_column_text > table > tbody > tr:nth-child('.$i.') > td:nth-child(1)')->filter('a')->each(function($node) use (&$row_url){ | |
$row_url[] = $node->attr('href'); | |
}); | |
} | |
//配列(出力データ)の宣言 | |
$rows = array(); | |
//urlデータ(配列)を1つずつ分解 | |
foreach($row_url as $url){ | |
//各URLの中身(データ)を入れつための配列及び変数 | |
$row = array(); | |
$img_url = null; | |
$img_name = null; | |
//URLを取得 | |
$row[] = $url; | |
//URLをリクエスト | |
$crawler = $client->request('GET', $url); | |
//画像の名前を取得 | |
$crawler->filter( '#js_async_main_column_name' )->each(function($node) use (&$row ,&$img_name){ | |
$img_name = $node->text(); | |
$row[] = $img_name; | |
}); | |
//画像URLデータを取得 | |
$crawler->filter('#js_async_main_column_text > div.temp_wrapper > div > div.temp_left > table > thead > tr > td > a')->each(function($node) use (&$row, &$img_url){ | |
$img_url = $node->attr('href'); | |
$row[] = $img_url; | |
}); | |
//必要な情報を集めたデータ(row)を出力データ(rows)に入れる | |
$rows[] = $row; | |
//画像のダウンロード | |
$data = file_get_contents($img_url); | |
file_put_contents('./'.$img_name.'.jpg',$data); | |
} | |
//CSVに出力 | |
$file = fopen('./img.csv','w'); | |
foreach($rows as $result){ | |
fputcsv($file, $result); | |
} | |
fclose($file); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment