简体   繁体   English

我如何使用LARAVEL将抓取的数据发送到Mysql表

[英]How can I send scraped DATA to Mysql table with LARAVEL

I scraped some data with php and I want to send it to Mysql from Laravel. 我用php抓取了一些数据,我想将其从Laravel发送到Mysql。 I connect to mysql but I don't know how to send it. 我连接到mysql,但不知道如何发送它。

here is my scraped code; 这是我的抓取代码;

$servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "laravel-test";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) 
    {

        die("Connection failed: " . $conn->connect_error);

    } 

    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,"https://suumo.jp/ms/shinchiku/osaka/sa_osaka/");
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1); 
    $sonuc = curl_exec($curl); 

    preg_match_all('@<div class="cassette-price">(.*?)</div>@si',$sonuc, $new);
    preg_match_all('@<ul class="cassette-plan">(.*?)</ul>@si',$sonuc, $info);
    preg_match_all('@<div class="cassette-header">(.*?)</div>@si',$sonuc, $name);


    $name = $name[0];
    $new = $new[0];
    $info = $info[0];


    for($i=0; $i < count($info);$i++)
    {
    echo $name[$i];
    echo $info[$i]; 
    echo $new[$i];  
    }

Thanks for help!!! 感谢帮助!!!

I am using laravel 5.7 by the way. 我正在使用laravel 5.7。

As far as I understand, you can just add the data to a regular table 据我了解,您可以将数据添加到常规表中

First, you can create a table (you've already done it, according to the comments) 首先,您可以创建一个表(根据注释,您已经完成了该表)

Then add configuration to Laravel [ https://laravel.com/docs/5.7/database] 然后将配置添加到Laravel [ https://laravel.com/docs/5.7/database]

The database configuration for your application is located at config/database.php . 您的应用程序的数据库配置位于config/database.php In this file you may define all of your database connections, as well as specify which connection should be used by default. 在此文件中,您可以定义所有数据库连接,并指定默认情况下应使用哪个连接。 Examples for most of the supported database systems are provided in this file. 此文件中提供了大多数受支持的数据库系统的示例。

Then use Laravel database to add the data 然后使用Laravel数据库添加数据

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Show a list of all of the application's users.
     *
     * @return Response
     */
    public function index()
    {
      for($i=0; $i < count($info);$i++)
      {
        DB::insert('insert into users (name, info, new)
        values (?, ?, ?)', [1, $name[$i], $info[$i], $new[$i] ]);

You could also add data to an array, and insert it once : 您也可以将数据添加到数组,然后将其插入一次:

for($i=0; $i < count($info);$i++)
{
           $data[] =[
                    'name' => $name[$i],
                    'info' => $info[$i],
                    'news' => $news[$i],
                   ];  
}
DB::insert($data);

(see Insert Multiple Records At Once With Laravel ) (请参阅使用Laravel一次插入多个记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM