【问题标题】:How to get data from a table on a webpage in PHP如何在 PHP 中从网页上的表格中获取数据
【发布时间】:2013-11-30 13:10:36
【问题描述】:

我正在尝试从网页中获取四行数据,每行有两行。经过一番阅读,我尝试了以下代码;

<?PHP

    require('simple_html_dom.php');
$ch = curl_init();
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
$target_url = 'http://www.boz.zm/(S(0m5hxtuuoex4xqjkzrpbsh55))/Startpage.aspx';
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$html = curl_exec($ch);
if (!$html) 
{
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    exit;
}
else
{
    echo "<br> Think the page was nabbed";
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXpath($dom);
    $tableData = array();

    foreach($xpath->query('//table[@id="_ctl0_zmain_Dg_ExchangeRates"]/tr[position()<5]') as $node)
    {
        $rowData = array();
        foreach($xpath->query('td', $node) as $cell)
        {
            $rowdat = $cell->textContent;
            $rowData[] = $rowdat;
        }
        $tableDate[]=$rowData;
    }
    print_r($tableData);

}


?>

只返回一个空数组。 我想将每一行的值放在一个多维数组中,以便我可以轻松地使用它们。关于如何完成这项任务的任何想法,即使它与我尝试做的不同方法我不介意。?提前致谢。

【问题讨论】:

  • 那么你的方法有什么问题?
  • 返回一个空数组。有什么想法吗?

标签: php regex dom curl xpath


【解决方案1】:

这只是一个错误的输入:你写的是:$tableDate[]=$rowData; 而不是$tableData[]=$rowData;

【讨论】:

  • @KaOma: 疲倦
猜你喜欢
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2014-05-16
相关资源
最近更新 更多