Goutte Web スクレイピング

複雑な Goutte PHP ファイル
goutte-manepa-keizai.php


63Pear Villageに戻る

<?php
require 'autoload.php';
use 
Goutte\Client;
//経済指標カレンダ
$top_page_url =
"https://www.moneypartners.co.jp/market/schedule/";
//経済指標カレンダーにアクセス
$client = new Client();

$client->setClient(
 new 
\GuzzleHttp\Client(
 array(
 
\GuzzleHttp\RequestOptions::VERIFY
  
=> false,)
 ));

$crawler $client
 
-> request('GET',$top_page_url);
$patern =
 
"|[0-9]{1,2}/[0-9]{1,2}\(|";
$crawler
 
-> filter('td')
//<td>を見付けるたびに実行  
 
-> each(
 function(
$e) use
  (&
$td,&$td0,&$frag,$patern,&$tr){
  
$text $e->text();
  switch(
$frag){
   case 
"3"://前回を取込
    
$td[] =
     
'<td class="las_z">'
     
.$text
     
."</td>";
    
$frag 2;
    break;
   case 
"2"://予想を取込
    
$td[] =
     
'<td class="las_ky">'
     
.$text
     
."</td>";
    
$frag 1;
    break;
   case 
"1"://結果を取込
    
$td[] =
     
'<td class="las_ky">'
     
.$text
     
."</td></tr>";
    
$frag 0;
  }
  
$hantei =
   
preg_match($patern,$text);
   if(
$hantei === 1){
    
$tr =
     
'<tr><td class="day">'
     
.$text
     
."</td>";
   }
//    echo $tr;
  
$b strpos($text,'(日)');
  
$b1 strpos($text,'(豪)');
  if(
$b === or $b1 === ){
   
$td[] = $tr;//日
   
$td[] = $td0[0];//時刻
   
$td[] =
    
'<td class="sihyo">'
    
.$text
    
.'</td>';//指標
   
$frag 3;
  }else{
//指標の2つ前の情報を暫定取得
   
$td0[0] = $td0[1];//時刻
   
$td0[1] =
    
'<td class="day">'
    
.$text
    
.'</td>';//時刻 or 重要度
  
}             
 }
//function END
 
);//each END
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>manepa</title>
<link async rel="stylesheet"
 href="css/goutte-kei.css">
</head>
<body>
<table class="kei">
<h3>経済指標カレンダー</h3>
<tr>
    <th class="day">日付</th>
    <th class="day">時刻</th>
    <th>指標</th>
    <th class="las_z">前回(訂正)</th>
    <th class="las_ky">予想</th>
    <th class="las_ky">結果</th>
</tr>
<?php
echo implode($td);
?>
</table>
</body>
</html>