Skip to content

Instantly share code, notes, and snippets.

@culy247
Last active May 24, 2021 10:07
Show Gist options
  • Select an option

  • Save culy247/2cc5a2de5a4bb58eee9cac38d4284d0c to your computer and use it in GitHub Desktop.

Select an option

Save culy247/2cc5a2de5a4bb58eee9cac38d4284d0c to your computer and use it in GitHub Desktop.
test crawler
<?php
namespace App\Console\Commands;
use Goutte\Client;
use Illuminate\Console\Command;
class Crawler extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crawler:run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$url = 'https://www.kaigokensaku.mhlw.go.jp/13/index.php?action_kouhyou_pref_seikatu_search_list_list=true&PrefCd=13&SearchType=life';
$client = new Client();
$params = ['method' => 'pager', 'action_kouhyou_pref_seikatu_search_list_result' => 'true', 'PrefCd' => '13', 'OriPrefCd' => '13', 'SearchType' => 'life', 'p_offset' => '10', 'p_count' => '2', 'p_sort' => '0', 'p_order' => '0', 'p_total' => '75', 'p_sort_name' => '26', 'p_order_name' => '1'];
$crawler = $client->request('POST', $url, $params);
//get body tag
$body = $crawler->filter('body')->first();
if ($body) {
//get first script yag
$script = $body->filter('script')->first();
if ($script) {
// decode html
$string = html_entity_decode($script->text());
$startStr = 'var aryJigyosyo = [];';
$endStr = '// 検索結果';
//position of start string
$startPos = strpos($string, $startStr);
//start substr index
$startIndex = $startPos + strlen($startStr) + 1;
//end post
$endPos = strpos($string, $endStr);
// substr (data string lenght)
$length = $endPos - $startIndex - 1;
$str = trim(substr($string, $startIndex, $length));
// replace to make json string
$str = str_replace('aryJigyosyo.push(', '', $str);
$str = str_replace(');', ',', $str);
$str = str_replace('\'', '"', $str);
$str = '['.trim($str, ',').']';
// decode json string to array
try {
$arr = json_decode($str, true);
dd($arr);
} catch (\Exception $e) {
dd($e);
}
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment