繁体   English   中英

使用简单的html dom从网页中提取值

[英]Extracting the value from webpage using simple html dom

我已经在网上搜索了,找到了使用简单的html dom提取数据的方法,但这给了我以下错误:

警告:file_get_contents( http://www.flipkart.com/moto-g-2nd-gen/p/itme6g3wferghmv3 ):无法打开流:HTTP请求失败! C:\\ Users \\ Abhishek \\ Desktop \\ editor \\ request \\ simple_html_dom.php中的HTTP / 1.1 500服务器错误

致命错误:在第9行的C:\\ Users \\ Abhishek \\ Desktop \\ editor \\ request \\ main.php中以布尔值调用成员函数find()

我为此设计的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>';

?>

我是该程序设计的新手,没有足够的知识,但是我的程序有错误吗?

确保启用了fopen包装器 。.从手册中

如果启用了fopen包装器,则可以将此功能用作URL的文件名。

由于禁用了此功能,因此file_get_contents()返回false会导致第二个错误。

服务器可能基于User-Agent拒绝了您的请求,请尝试使用curl获取页面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>';

输出:

卢比 10,999


注意:

我可以确认服务器基于User-Agent拒绝了您的请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM