LoginSignup
2
1

More than 3 years have passed since last update.

PHPでスクレイピングその1

Last updated at Posted at 2020-03-10

Goutte

PHPで簡単にスクレイピングを行うことができるライブラリーです。

導入

コンポーザーでインストール

$ composer require fabpot/goutte

もしくは上記Githubから.pharファイルをダウンロードしましょう

あとはソース内で

test.php
require_once './vendor/autoload.php';

test.php
require_once 'goutte.phar';

のように読み込んであげて、

test.php
use Goutte\Client;

useしてあげるだけでOKです!

使ってみる

https://giftpad.co.jp/company こちらのサイトから

赤枠の部分、良い言葉なので是非PHPで取得したいですね

campany.png

XPathを確認して、
xpath.png

ソースに反映

index.php
<?php

require_once 'goutte.phar';

use Goutte\Client;

// Goutteオブジェクトの生成
$client = new Client();

// ページ情報を取得
$crawler = $client->request('GET', 'https://giftpad.co.jp/company');

// タグを指定
$word = $crawler->filter('div#content div section div div div p')->each(function($element){
    return $element->text();
});

echo $word[0];

取れました!
get.png

総評

  • 簡単に導入できて、XPathやタグの指定で直感的にスクレイピングしやすいと感じました。
  • 今回は簡単な紹介でしたが、次回は取得したデータをもとにして何かのツールを作ろうと思います。
2
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
2
1