ウェブ上の必要なデータを抽出する方法-スクレイピング-


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

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

え~と、ネタ切れです。。。今更なんですがスクレイピングをネタにしようかな~なんて。。。
書くネタあるんですけど、ちょっとまとめるのに時間がかかりそうなものばかりなんでちゃちゃっと書けそうなものを選びました。。
PHP Simple HTML DOM Parserについて書きたいと思います。

かなり出回っているのでかなり情報があると思います。
でもこれはほんといいっすよー!

イメージで言ったらウェブ上の必要な箇所をザクっと切り出してくれるという感じですね。
無許可で人のサイトを取り出さないようにしてくださいね。


スポンサーリンク

PHP Simple HTML DOM Parserの使い方

まずここからダウンロードしてきてください。
そしてダウンロードしてきたものを解凍して「simple_html_dom.php」このファイルだけをアップします。他のファイルはマニュアルとかサンプルなので必要な応じてお使いください。

1
2
3
4
5
6
7
8
9
10
11
//ファイルを読み込みます
require_once('simple_html_dom.php');
// file_get_htmlで取り込みたいURLを指定してやります。例で朝日新聞のヘッドライン部分です。
$html = file_get_html('http://www.asahi.com/');
// findメソッドで取り込みたい場所を指定してやります。jQueryっぽい指定ができてとても簡単!
foreach($html->find('#HeadLine .Guest li') as $list) {
            echo $list->outertext;
        }
//クリアーして開放してやります!
$html->clear();
unset($html);

これでヘッドライン取得できます!めちゃ簡単ですね!

ただこれだと文字化けする可能性があるのとヘッドラインの一番上の部分が取得できていないのです。

そして下記のようにするとうまくいきます。

1
2
3
4
5
6
7
8
9
10
11
12
13
require_once('simplehtmldom/simple_html_dom.php');
$html = file_get_html('http://www.asahi.com/');
mb_language('Japanese');
$url = 'http://www.asahi.com/';
$contents = file_get_contents($url);
$content = mb_convert_encoding($contents, 'UTF-8', 'auto');
$html = str_get_html($content);
echo $html->find('.Guest dl.FstTitle', 0)->outertext;
        foreach($html->find('#HeadLine .Guest li') as $list) {
            echo $list->outertext;
        }
        $html->clear();
        unset($html);

URLやファイルから要素を取得する方法

・HTMLを読み込む場合はstr_get_html

str_get_html('<html><body><h1>HelloWorld</h1><p>テストです!</p></body></html>');

・URLやファイルを読み込む場合はfile_get_html

file_get_html('https://php-fan.org/');

オブジェクトを使っての読み込みもできます!

$html = new simple_html_dom();

・オブジェクトにした時のHTMLを読む込む場合はload

$html->load('<html><body><h1>HelloWorld</h1><p>テストです!</p></body></html>');

・オブジェクトにした時のファイルやURLから読み込む場合はload_file

$html->load_file('https://php-fan.org/');

・要素を指定して取り込む

$html->find('#sample ul li');
$html->find('#test a');

取得した要素の出力方法

1
$html->load('<div id="sample"><p>サンプルです。</p><dl class="dl-horizontal"><dt>2012年6月14日</dt><dd>テストです。</dd></dl></div>')

・plaintext
単純にテキストのみです。タグなどは取り除かれます。

1
2
echo $html->find('#sample', 0)->plaintext;  
=> サンプルです。2012614日テストです。

・innertext
指定した要素の内側を取り出します。下記の例の場合だと#sampleは含まれないことです。

1
2
echo $html->find('#sample', 0)->innertext;  
=> <p>サンプルです。</p><dl class="dl-horizontal"><dt>2012614</dt><dd>テストです。</dd></dl>

・outertext
指定した要素を含む全体を出力します。下記の例では#sampleを含む以下を出力します。

1
2
echo $html->find('#sample', 0)->outertext;
=> <div id="sample"><p>サンプルです。</p><dl class="dl-horizontal"><dt>2012614</dt><dd>テストです。</dd></dl></div>

いろいろな使い方があります。詳しくはマニュアルを見るとわかりやすいです。

→応用編の記事はコチラ
マニュアルはコチラ

デモサイトは当サイトの人気記事を取り込んでいます。

デモサイト

3件のコメント

コメントを残す

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