【问题标题】:How to insert extracted data to website如何将提取的数据插入网站
【发布时间】:2017-05-22 20:43:21
【问题描述】:

我想知道如何将数据插入到我从其他网站提取的数据库表中

我的数据库表结构是:

mysqli_query($db, "INSERT into doctors(name, spec, qualification, image) VALUES('$name', '$spec', '$qua', '$image'));

这是我从外部提取数据的 php 代码

<?php
    require('admin/inc/simple_html_dom.php');
    require('admin/inc/db.php');
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => "http://health.hamariweb.com/rawalpindi/doctors",
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    ));
    $file = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);
    $dom = new simple_html_dom();
    $dom->load($file);
    $doctorDivs = $dom->find("#infinite-grid-images", 0)->children();
    $doctors = array();
    foreach($doctorDivs as $div){
        $doctor = array();
        $image = $doctor["image"] = "http://health.hamariweb.com".$div->find('img', 0)->src;
        $details = $div->find('table', 1)->find("tr");
        $name = $doctor["name"] = trim($details[0]->plaintext);
        $spec = $doctor["Spec"] = trim($details[1]->plaintext);
        $qua = $doctor["etc"] = trim($details[2]->plaintext);
        $doctors[] = $doctor;
        while($doctors){
            mysqli_query($con, "INSERT into doctors(name, spec, qualification, image) VALUES('$name', '$spec', '$qua', '$image')");
        }
    }
    echo "<pre>";
    var_dump($doctors);


?>

【问题讨论】:

  • 请向我们展示一些要处理的实际数据,向我们展示您的数据库表结构,向我们展示您希望看到的内容,向我们展示自我解决的尝试。

标签: php html curl dom


【解决方案1】:

从循环中取出 $doctors[] 数组。我对您的脚本进行了一些更改,并且有效。

require('admin/inc/simple_html_dom.php');
//require('admin/inc/db.php');
$db = new mysqli('localhost', DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($db, "utf8");

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "http://health.hamariweb.com/rawalpindi/doctors",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
));
$file = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
$dom = new simple_html_dom();
$dom->load($file);
$doctorDivs = $dom->find("#infinite-grid-images", 0)->children();
$doctors = array();
foreach($doctorDivs as $div){
    $doctor = array();
    $image = $doctor["image"] = "http://health.hamariweb.com".$div->find('img', 0)->src;
    $details = $div->find('table', 1)->find("tr");
    $name = $doctor["name"] = trim($details[0]->plaintext);
    $spec = $doctor["Spec"] = trim($details[1]->plaintext);
    $qua = $doctor["etc"] = trim($details[2]->plaintext);
    $doctors[] = $doctor;

}
//insert database part.
$data = $doctors;
foreach ($data as $val) {

    $sql = "INSERT IGNORE INTO doctors (id, name, spec, qualification, image) VALUES (NULL, '".$val["name"]."' , '" .$val["Spec"]. "' , '" .$val["etc"]. "' , '" .$val["image"]. "');";
    mysqli_query($db,$sql);

}

mysqli_close($db);

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2020-07-13
    • 2018-12-10
    • 2017-10-16
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多