スクレイピングとは
ウェブサイトから情報を抽出するコンピュータソフトウェア技術のこと。
引用:Wikipedia
phpQueryの導入
公式サイトからファイルをダウンロードする。
解凍後、プロジェクト内の好きな位置に置いてください。
Step.1
phpQueryを読み込む
index.php
<?php
require_once("./phpQuery-onefile.php");
?>
パスはphpQuery-onefile.phpを置いた位置に合わせてください。
Step.2
webサイトを読み込む
index.php
<?php
require_once("./phpQuery-onefile.php");
$html = file_get_contents("https://ja.wikipedia.org/wiki/%E3%82%A6%E3%82%A7%E3%83%96%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0");
?>
URLをfile_get_contentsの中に記入します。
Step.3
欲しい要素を取得します。
index.php
<?php
require_once("./phpQuery-onefile.php");
$html = file_get_contents("https://ja.wikipedia.org/wiki/%E3%82%A6%E3%82%A7%E3%83%96%E3%82%B9%E3%82%AF%E3%83%AC%E3%82%A4%E3%83%94%E3%83%B3%E3%82%B0");
echo phpQuery::newDocument($html)->find("h1")->text();
?>
こんな感じで表示されます!