2015-05-11 102 views
1

我尋覓了網,發現用簡單的HTML DOM提取數據的方式,但它給我下面的錯誤中提取從網頁上的價值:使用簡單的HTML DOM

Warning: file_get_contents(http://www.flipkart.com/moto-g-2nd-gen/p/itme6g3wferghmv3): failed to open stream: HTTP request failed! HTTP/1.1 500 Server Error in C:\Users\Abhishek\Desktop\editor\request\simple_html_dom.php on line 75

Fatal error: Call to a member function find() on boolean in C:\Users\Abhishek\Desktop\editor\request\main.php on line 9

它我設計的PHP代碼:

<?php 

include('simple_html_dom.php'); 

$html = file_get_html('http://www.flipkart.com/moto-g-2nd-gen/p/itme6g3wferghmv3'); 


foreach($html->find('span.selling-price.omniture-field') as $e) 
    echo $e->outertext . '<br>'; 

?> 

我在這個編程中有一個新東西,但沒有足夠的知識,但在我的程序中是否有任何錯誤?

回答

3

服務器可能拒絕基於用戶代理您的要求,請嘗試使用捲曲獲得頁面的HTML,即

<?php 
$url="http://www.flipkart.com/moto-g-2nd-gen/p/itme6g3wferghmv3"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_ENCODING, ""); 
$pagebody=curl_exec($ch); 
curl_close ($ch); 

include('simple_html_dom.php'); 
$html = str_get_html($pagebody); 

foreach($html->find('.selling-price') as $e) 
    echo $e->outertext . '<br>'; 

輸出:

盧比。 10999


注:

我可以確認服務器是基於用戶代理拒絕你的要求。

+0

還是不退貨的。 –

+0

我已經更新了我的答案,如果它對您有幫助,請考慮投票1+,並通過點擊投票箭頭中間的複選標記tks接受它作爲正確答案! –

+0

是的,它工作..上次我在URL中犯了一個錯誤.. –

4

確保fopen wrappers被使能做到這一點。從the manual

A URL can be used as a filename with this function if the fopen wrappers have been enabled.

由於這種被禁用file_get_contents()回報false這會導致你的第二個錯誤的結果。