简体   繁体   English

将 htmldom 结果存储在 mysql 数据库中

[英]Store htmldom results in mysql database

I try to store scraped data into my database.我尝试将抓取的数据存储到我的数据库中。 just to echo the result works perfectly, so the scraping is OK.只是为了完美地呼应结果,所以抓取是可以的。 But there must be an error in defining a variable and passing it to the mysql-insert.但是在定义变量并将其传递给mysql-insert 时一定有错误。 I get the message "New record created successfully".我收到消息“新记录创建成功”。 The result for the variable is empty and the date is there.变量的结果是空的,并且日期在那里。

<?php

$html = file_get_contents('https://www.marketwatch.com/market-data/us?mod=market-data-center');

$scriptDocument = new DOMDocument();

libxml_use_internal_errors(TRUE); 

if(!empty($html)){ 

    $scriptDocument->loadHTML($html);

    libxml_clear_errors(); 

    $scriptDOMXPath = new DOMXPath($scriptDocument);
    $scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){echo $row->nodeValue;}} // echo result works
    $scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$row->nodeValue = $nasdaq_dec;}} // defining variable does not work

};

    $host_name = '';
    $database = '';
    $user_name = '';
    $password = '';
    
    try {
    $conn = new PDO("mysql:host=$host_name; dbname=$database;", $user_name, $password);      
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO `morgenroutine` (date,nasdaq_dec)
    VALUES (NOW(), '$nasdaq_dec')";

    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;

?>

Now I got it: I changed the format in mysql to "text" and the line to: $scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$nasdaq_dec = $row->nodeValue;}}现在我明白了:我将 mysql 中的格式更改为“文本”,并将行更改为: $scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$nasdaq_dec = $row->nodeValue;}}

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

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