Goutte Web スクレイピング

単純な Goutte PHP ファイル
scraping-mmlbbs21.php


63Pear Villageに戻る

<?php
// Composerのオートロードを有効にする
require 'autoload.php';
use 
Goutte\Client;
// 対象URL
$url =
 
"https://sakuramml.com/mmlbbs6/";
// Goutte\Clientの生成
//$client = new Goutte\Client();
$client = new Client();
//XAMPPから実行するときは
$client->setClient(new \GuzzleHttp\Client(
 array(
 
\GuzzleHttp\RequestOptions::VERIFY
  
=> false,)
 ));
/* 
又は
$client->setClient(new \GuzzleHttp\Client(
[\GuzzleHttp\RequestOptions::VERIFY
  => false,]
 ));
 PHP5.4 以降は配列に[ ]が使える
 Dreamweaver CS6 では
 エラ表示 Syntax Error がでる
*/
$crawler $client->request('GET'$url);
// CSSセレクタを指定して要素を取り出す

$crawler
 
-> filter('table tr')
 -> 
each(function($e) {
 
// <tr>を見つけるたびに実行される部分
  
$td $e -> filter('td');
  
$count $td -> eq(0) -> text();
  
$title $td -> eq(1) -> text();
  
$link  "";
  
$o $td
   
-> eq(1) -> filter('a')
   -> 
extract(array('href'));
  if (
$o) { $link $o[0]; }
  
$author $td
   
-> eq(2) -> text();
//echo "$title,$author,$link\n";
  
echo
   
$title."<br>&emsp;".
   
$author.
   
"<br>&emsp;&emsp;".
   
$link."<br><br>";
  });
?>