0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

オリジナルアプリ作成進捗(スクレイピング処理記述もあと少し)

Last updated at Posted at 2022-06-21

オリジナルアプリ作成も終盤に入り、スクレイピング処理の記述ももう少しのところまで来ています。

スクレイピング処理のコードはこんな感じです。

ScrapingController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Goutte\Client;
use App\Models\Playerdata;


class ScrapingController extends Controller
{
    public function scraping1()
    {
        $client = new Client();
        $crawler = $client->request('GET', 'https://baseball.yahoo.co.jp/npb/teams/3/memberlist?kind=p');
        $info = $crawler->filter('.bb-playerTable__row')->each(function ($tr) {
        $data = $tr->text();
      
        return $data;
        });
        foreach($info as $data) {
            
            // $playerdata = new Playerdata;
            // $playerdata->playername = $data[1];
            $playerdata = explode(" ", $data);
            // print_r($playerdata);
            dump($playerdata[4]);
            
            $playerdata = Playerdata::select('playername', 'ining', 'balls', 'hit_by_a_pitch', 'by_homeruns', 'wins', 'loses', 'saves', 'resp_points', 'lost_points', 'saved_adv')->get();
            // dd($playerdata);
            // $playerdata->save();
        };
        return view('pitcher');
    }    
    
    
     public function scraping2()
     {
        $client = new Client();
        $crawler = $client->request('GET', 'https://baseball.yahoo.co.jp/npb/teams/3/memberlist?kind=b');
        $info = $crawler->filter('.bb-playerTable__row')->each(function ($tr) {
        $data = $tr->text();
      
        return $data;
        });
        foreach($info as $data) {
            $playerdata = explode(" ", $data);
            //print_r($playerdata) ;
            dump($playerdata[4]);
            
            $playerdata = Playerdata::select('playername', 'times_at_but', 'hit', 'hit_point', 'hit_adv')->get();
            
            // $playerdata->save();
            
        };
        return view('butter');
    }
    
}

未完ではありますが、ここまで書きました。

ここから分からないこと

上のコードだと、データを持ってきたときに、すべてのデータを取得できずデータベースの追加・更新が出来ません。

データベースの追加・更新が出来るコードが書ければ、ほぼ完成にはなるんですが。(これがわからない)

みなさんはお分かりになるでしょうか?

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?