2015-05-11 103 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这会导致你的第二个错误的结果。