スクレイピング応用編-phpQuery-


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

さて今回は前回に引き続きスクレイピングします。といっても前回よりかなり応用が効くやり方です。スクレイピングだけじゃないですがDOM操作もできちゃうすぐれものphpQueryをご紹介します。
ただ取ってくるだけじゃないですよ。色々と便利に使える要素がかなり詰まっています!
→前回の記事はコチラ

詳しくはコチラをご参照ください→phpQuery

マニュアルはコチラ


スポンサーリンク


ダウンロードサイトからダウンロードします。(full-packageかone-fileどちらでも良いですがfull-packageが良いと思います。下記のことだけならone-fileでもできます。)
require_once(‘phpQuery/phpQuery.php’)
で呼び出して使います。

phpQuery設定方法

1
2
//ファイル読み込み
$html = phpQuery::newDocumentFile('phpquerydemo.html');

読み込むときのメソッドですがその他たくさんありますが必要に応じて使ってください。
基本的にphpQuery::newDocument($html, $contentType = null)かphpQuery::newDocumentFile($file, $contentType = null)で充分だと思いますね。

1
2
3
4
5
6
7
8
9
10
phpQuery::newDocument($html, $contentType = null)
phpQuery::newDocumentFile($file, $contentType = null)
phpQuery::newDocumentHTML($html, $charset = 'utf-8')
phpQuery::newDocumentXHTML($html, $charset = 'utf-8')
phpQuery::newDocumentXML($html, $charset = 'utf-8')
phpQuery::newDocumentPHP($html, $contentType = null)
phpQuery::newDocumentFileHTML($file, $charset = 'utf-8')
phpQuery::newDocumentFileXHTML($file, $charset = 'utf-8')
phpQuery::newDocumentFileXML($file, $charset = 'utf-8')
phpQuery::newDocumentFilePHP($file, $contentType)

そして、
pq($param, $context = null);
オブジェクトに変換する関数を使うんですね。基本的にはこれだけです。細かい取得方法はjQueryと一緒です。
試しに使ってみましょう!

タイトル取得

1
2
$title = $html['title'];
echo '取得してきたサイトのタイトル:' . $title->text();

上記の例はタイトルタグを取得しています。出力はテキストですね。このあたりはjQueryっぽい感じですよね

h1~h6のテキスト取得

1
2
$header = $html[':header'];
echo 'h1~h6テキスト:' . $header->text();

[]の中はjQueryのセレクタやフィルターを使って要素を取り出します。

一番初めのul要素を取得します。

1
2
3
4
5
$lists = $html['ul:first'];
 
foreach ($lists as $list) {
    echo pq($list)->htmlOuter(); 
}

上記も見ていればわかりますよね。htmlOuter()は指定した要素も含みます。

クラス追加してリスト表示します。

1
2
3
4
5
6
$lists = $html['ul#blog'];
 
foreach ($lists as $list) {
    echo pq($list)->addClass('list');
 
}

クラスを追加しています。

articleタグ以下を取得して表示します。

1
2
3
4
5
$articles = $html['article'];
foreach ($articles as $article) {
    echo pq($article)->html();
 
}

html()は指定した要素を除いた下の要素になります。

imgのurlを表示します。

1
2
3
4
foreach ($html->find('img') as $img){
    $src = $img->getAttribute('src');
    echo $src,'<br>';    
}

直感的にわかりますよね。

一番初めのa要素のアドレスを取得

1
echo $html['a']->attr('href');

jQueryやっていればおなじみの感じです。

最初のdl要素dl-horizontalクラスののdtの個数を数えます。

1
echo count(pq('dl.dl-horizontal > dt'));

指定リストのみ取得

1
2
3
4
5
$footers = $html['ul#footer_menu']->find('li:eq(1)');
foreach ($footers as $footer) {
    echo pq($footer)->html();
 
}

ざざっとサンプルを上げてみましたが何となくわかると思います。
デモサイトを参照して確かめてください。

デモサイト

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です