Created
November 17, 2015 17:55
-
-
Save ko31/93e9b1863f669aca49b5 to your computer and use it in GitHub Desktop.
【PHP】Adventarの登録状況をスクレイピング
This file contains hidden or 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 | |
// コマンドラインのみ | |
if (php_sapi_name() != 'cli') { | |
exit('Service Unavailable'); | |
} | |
require_once 'goutte.phar'; | |
use Goutte\Client; | |
$client = new Client(); | |
// Adventarの2015年一覧ページをGET | |
$crawler = $client->request('GET', 'http://www.adventar.org/calendars?year=2015'); | |
// カレンダーリスト部分を取得 | |
$dom = $crawler->filter('div.mod-calendarList ul li'); | |
$dom->each(function ($node) use (&$records) { | |
// タイトル | |
$name = $node->filter('div.mod-calendarList-title')->text(); | |
// URL | |
$url = $node->filter('div.mod-calendarList-title a')->attr('href'); | |
// 背景色 | |
$style = $node->filter('div.mod-calendarList-title a')->attr('style'); | |
// 登録件数 | |
$count = $node->filter('div.mod-calendarList-body div.mod-calendarIndicator div.mod-calendarIndicator-value')->attr('data-count'); | |
$records[] = array( | |
'name' => trim($name), | |
'url' => $url, | |
'style' => trim(str_replace('background:', '', $style)), | |
'count' => $count, | |
); | |
}); | |
$records = array( | |
'name' => 'adventar', | |
'children' => $records | |
); | |
// json出力 | |
file_put_contents('adventar.json', json_encode($records)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment