gpt4 book ai didi

html - PHP - DOM,从 html 中剥离表格

转载 作者:太空宇宙 更新时间:2023-11-04 15:17:16 25 4
gpt4 key购买 nike

<分区>

我有一个这样结束的 curl 脚本:

  $data = curl_exec($ch);

curl_close($ch);

return $data;

}

$data 字符串是一个带有表格的 HTML 页面,我想删除它以便我可以将数据存储到 MYSQL 数据库中,我尝试使用 DOM 和命令,例如:

  // new dom object
$dom = new DOMDocument();

//load the html
$html = str_get_html($returned_content2);
$dom->strictErrorChecking = false;


//discard white space
$dom->preserveWhiteSpace = false;

//the table by its tag name
$tables = $dom->getElementsByTagName('table');

//get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');

// loop over the table rows
foreach ($rows as $row)
{
// get each column by tag name
$cols = $row->getElementsByTagName('td');
// echo the values
echo $cols->item(0)->nodeValue.'<br />';
echo $cols->item(1)->nodeValue.'<br />';
echo $cols->item(2)->nodeValue;
}
}

但一直报错:

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/sdsd/dfdsfsdfds/sdfsdfs/table.php on line 178

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com