如何使用 LARAVEL 将抓取的数据发送到 Mysql table
How can I send scraped DATA to Mysql table with LARAVEL
我用 php 抓取了一些数据,我想将它从 Laravel 发送到 Mysql。我连接到 mysql,但我不知道如何发送。
这是我抓取的代码;
$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];
}
感谢帮助!!!
顺便说一句,我正在使用laravel 5.7。
据我了解,您可以将数据添加到常规 table
首先,你可以创建一个table(你已经做了,根据评论)
然后添加配置到Laravel [https://laravel.com/docs/5.7/database]
The database configuration for your application is located at
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.
然后使用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] ]);
您还可以将数据添加到数组中,然后插入一次:
for($i=0; $i < count($info);$i++)
{
$data[] =[
'name' => $name[$i],
'info' => $info[$i],
'news' => $news[$i],
];
}
DB::insert($data);
(参见 Insert Multiple Records At Once With Laravel)
我用 php 抓取了一些数据,我想将它从 Laravel 发送到 Mysql。我连接到 mysql,但我不知道如何发送。
这是我抓取的代码;
$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];
}
感谢帮助!!!
顺便说一句,我正在使用laravel 5.7。
据我了解,您可以将数据添加到常规 table
首先,你可以创建一个table(你已经做了,根据评论)
然后添加配置到Laravel [https://laravel.com/docs/5.7/database]
The database configuration for your application is located at
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.
然后使用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] ]);
您还可以将数据添加到数组中,然后插入一次:
for($i=0; $i < count($info);$i++)
{
$data[] =[
'name' => $name[$i],
'info' => $info[$i],
'news' => $news[$i],
];
}
DB::insert($data);
(参见 Insert Multiple Records At Once With Laravel)