我想從雅虎刮一些數據,但xpath查詢返回我的長度爲0當我var_dump
這個。這是我的一部分刮碼。我的xpath查詢沒有返回任何結果
error_reporting(0);
function curl($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)');
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, false);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 200);
return curl_exec($curl);
}
$page = curl('https://www.yahoo.com');
$dom = new DOMDocument();
$dom->loadHTML($page);
$xpath = new DOMXPath($dom);
$link = $xpath->query('//li[@style="background-color:#fafaff;"]/div/div/div/h3/a');
foreach ($link as $links) {
$get_title[] = $links->nodeValue;
$get_link[] = $links->getAttribute('href');
}
此代碼沒有語法錯誤,但存在邏輯錯誤。
刪除錯誤報告0,您的輸出在哪裏?你輸出什麼而看不到?你沒有任何回聲或打印,你怎麼知道它不工作 –
偷廣告,其中有第一個 – 2016-03-02 21:36:01
它再次不工作..我刪除錯誤報告0 – coder32