【问题标题】:Scraper returns empty arrayScraper 返回空数组
【发布时间】:2014-07-13 15:43:09
【问题描述】:

我对 curl 和 xpath 有点陌生,所以还在学习 in's 和 out's。我写了一个刮刀,但是当我尝试通过数组显示刮取的数据时,什么也没有出现。那么我的代码有什么问题呢?

<?php

ini_set("display_errors", "1");
error_reporting(-1);
error_reporting(E_ERROR);
libxml_use_internal_errors(true);

//Basic Function
function get_url_contents($url, $timeout = 10, $userAgent = 'Mozilla/5.0(Macintosh; U; Intel Mac OS X 10_5_8; en-US)AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10'){
    $rawhtml = curl_init();//handler
    curl_setopt($rawhtml, CURLOPT_URL,$url);//url
    curl_setopt($rawhtml, CURLOPT_RETURNTRANSFER, 1);//return result as string rahter than direct output
    curl_setopt($rawhtml, CURLOPT_CONNECTTIMEOUT,$timeout);//set timeout
    curl_setopt($rawhtml, CURLOPT_USERAGENT,$userAgent);//set user agent
    $output = curl_exec($rawhtml);//execute curl call
    curl_close($rawhtml);//close connection

    if(!$output){
        return -1;//if nothing obtained, return -1
    }
    return $output;
}

//get raw html
$html_string = get_url_contents("http://www.beursgorilla.nl/fonds-informatie.asp?naam=Aegon&cat=koersen&subcat=1&instrumentcode=955000020");//url here
//load HTML into DOM object
//ref http://www.php.net/manual/en/domdocument.loadhtml.php
//note html does not have to be well fpr,ed with this function

$dom_object = new DOMDocument();
@$dom_object->loadHTML($html_string);

//perform Xpath queries on DOM
//ref http://www.php.net/manual/en/domxpath.query.php

$xpath = new DOMXPath($dom_object);

//perform Xpath query
//use any specfic property to narrow focus

$nodes = $xpath->query("//table[@class='maintable']/tbody/tr[4]/td[2]/table[@class='koersen_tabel']/tbody/tr[2]/td[@class='koersen_tabel_midden']");

//setup some basic variables

$i = -1; //$i = counter

//when process nodes as below, cycling trough
//but not grabbing data from the header row of the table

$result = array();

//preform xpath subqueries to get numbers

foreach($nodes as $node){
    $i++;
    //using each 'node' as the limit for the new xpath to search within
    //make queries relative by starting them with a dot (e.g. ".//...")

    $details = $xpath->query("//table[3]/tbody/tr/td[1]/table[@class='fonds_info_koersen_links']/tbody/tr[1]/td[2]", $node);
    foreach($details as $detail){
        $result[$i][''] = $detail->nodeValue;
    }

    $details = $xpath->query("//table[3]/tbody/tr/td[1]/table[@class='fonds_info_koersen_links']/tbody/tr[4]/td[2]", $node);
    foreach($details as $detail){
         $result[$i][''] = $detail->nodeValue;
    }

    if(curl_errno($rawhtml)){
        echo 'Curl error: ' . curl_error($rawhtml);

        print'<pre>';   
        print_r($result);
        print '</pre>';
    }
}

?>

我已经通过 Chrome 的元素检查器检查了 xpath 查询,它们似乎是正确的。我真的不知道代码有什么问题。

【问题讨论】:

  • 使用更多echo 来查看脚本中发生了什么 - 打印所有变量以及哪些if/foreach 被执行。

标签: php curl xpath web-scraping domdocument


【解决方案1】:

这行代码呢?

$result[$i][''] = $detail->nodeValue;

这不应该是这样的吗:

$result[$i][] = $detail->nodeValue;

(看方括号)

【讨论】:

    【解决方案2】:

    我已经重写了我的爬虫并使用了 PHP Simple HTML DOM Parser。这解决了我的问题,现在一切正常:)。

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 1970-01-01
      • 2016-03-31
      • 2017-07-12
      • 1970-01-01
      • 2021-08-11
      • 2017-02-27
      • 2020-04-01
      • 2019-04-17
      相关资源
      最近更新 更多