LoginSignup
5
1

More than 5 years have passed since last update.

【PHP】3行クッキングでwebスクレイピングする

Posted at

スクレイピングとは

ウェブサイトから情報を抽出するコンピュータソフトウェア技術のこと。

引用: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

欲しい要素を取得します。

今回はこの部分を取得します。
スクリーンショット 2018-10-13 19_Fotor.png

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();
?>

こんな感じで表示されます!

localhost_8080_tenki_public__Fotor.png

終わり

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1