Rumah > Soal Jawab > teks badan
Saya cuba menyimpan data yang dikikis ke dalam pangkalan data saya. Ia hanya mengeluarkan keputusan, jadi merangkak adalah normal. Tetapi pasti terdapat ralat dalam menentukan pembolehubah dan menghantarnya ke mysql-insert. Saya mendapat mesej "Rekod baharu berjaya dibuat". Hasil pembolehubah kosong dan tarikh wujud.
<?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 "成功创建新记录"; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
P粉2695300532023-09-16 09:28:43
Sekarang saya faham! Saya menukar format kepada "teks" dalam mysql dan menukar baris kepada:
$scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]/following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$nasdaq_dec = $row->nodeValue;}}