抓取 HTML 表并将行数据插入数据库表

问题描述 投票:0回答:1

我使用 SimpleHtmlDom 生成了结果。

include('simple_html_dom.php');
$html = file_get_html('http://www.example.com');

$count =count($html->find('table tbody tr td')) -

1; //$计数=170

for ($i = 1; $i < $count; $i++) {
    echo $val[$i] = $html->find('table tbody tr td', $i)->plaintext;
}  

我不想要第一个和最后一个输出,所以使用了

$x = 1 and $count - 1

输出结果为

enter image description here

这里,每一行的股票代码都是相同的,并且每第 8 个数组值后面有一个循环。

我的mysql表是记录,其中列为“

id,date,stock_symbol,buyer,seller,quantity,rate,amount

"INSERT INTO records 
(id,date,stock_symbol,buyer,seller,quantity,rate,amount) 
VALUE ('$val1','$val2','$val3','$val4','$val5','$val6','$val7','$val8')",
('$val9','$val10','$val11','$val12','$val13','$val14','$val15','$val16')"

如果数量少的话我可以手动完成,但数量超过了几百个。

我试过了

$values = array('English', 'Arabic');
$statement = 'INSERT INTO contact (`language1`, `language2`) VALUES ("' . implode('", "', $values) . '")';
echo $statement;

来自如何将数组值一一插入到MySQL表的不同列中,但是我的数组没有逗号(,),没有得到任何结果

编辑:在 ans 后编辑

include('inc/simple_html_dom.php');
include('mysql.php');
// get DOM from URL or file

header('Content-Type: text/html; charset=utf-8');
// get DOM from URL or file
$html = file_get_html('http://www.seismonepal.gov.np/index.php?action=earthquakes&show=recent');

// remove all image
foreach($html->find('div [class=block2-content] span') as $e)
$array=  str_replace(' ', '', $e->plaintext);

//split all you data into the size chunks you want.
//$array = array('English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French');
$chunks = array_chunk($array, 7);

//get the amount of chunks you have.
$c = count($chunks);
$a = 0;

//then run through each chunk entering it into the database.

do{
    $values = null;
    $x = 1;

    foreach ($chunks[$a] as $value) {
        $values .= "?";
        if ($x < count($chunks[$a])) {
            $values .= ', ';
        }

        $x++;
    }

    $sql = "INSERT INTO earthquake(id,date_np,local_time,lattitude,longitude,magnitude,epic_center,remarks) VALUES ({$values})";    
    $query = $db->prepare($sql);

    if (count($chunks[$a])) {
        $y = 1;
        foreach ($chunks[$a] as $param) {
            $query->bindValue($y, $param);
            $y++;
        }    
    }

    if ($query->execute()) {
        echo "Done {$a}" . "<br/>";
    } else {
        echo "Not Done {$a}" . "<br/>";
    }
    $a++;  

} while ($a < $c);
mysql arrays web-scraping bulkinsert simple-html-dom
1个回答
1
投票

这将进行多个查询,但这是您可以做到的一种方式。

将所有数据分割成您想要的大小块。

$array = array('English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French', 'Dutch', 'English', 'French');
$chunks = array_chunk($array, 2);

获取您拥有的块数量。

$c = count($chunks);
$a = 0;

然后运行每个块,将其输入数据库。

do{
    $values = null;
    $x = 1;

    foreach($chunks[$a] as $value) {
        $values .= "?";
        if($x < count($chunks[$a])) {
            $values .= ', ';
        }

        $x++;
    }

    $sql = "INSERT INTO `table` (`language1`, `language2`) VALUES ({$values})";    
    $query = $db->prepare($sql);

    if(count($chunks[$a])){
        $y = 1;
        foreach($chunks[$a] as $param) {
            $query->bindValue($y, $param);
            $y++;
        }    
    }


    if($query->execute()){
        echo "Done {$a}" . "<br/>";
    }else{
        echo "Not Done {$a}" . "<br/>";
    }
    $a++;  

}while($a < $c);

希望这有帮助。

编辑后续问题。

当你找到它们时,你可以将它们放入一个数组中。

$array = array();

foreach($html->find('div [class=block2-content] span') as $e){
    array_push($array, $e->plaintext);
}

然后您可以使用

array_chunk

© www.soinside.com 2019 - 2024. All rights reserved.