【问题标题】:Extracting the value from webpage using simple html dom使用简单的 html dom 从网页中提取值
【发布时间】:2015-05-11 17:56:35
【问题描述】:

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

警告: 文件获取内容(http://www.flipkart.com/moto-g-2nd-gen/p/itme6g3wferghmv3): 无法打开流:HTTP 请求失败! HTTP/1.1 500 服务器错误 在 C:\Users\Abhishek\Desktop\editor\request\simple_html_dom.php 上 第 75 行

致命错误:在布尔值中调用成员函数 find() C:\Users\Abhishek\Desktop\editor\request\main.php 在第 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>';

?>

我是这个编程的新手,没有足够的知识,但我的程序有什么错误吗?

【问题讨论】:

    标签: php html web-scraping file-get-contents


    【解决方案1】:

    确保启用fopen wrappers 来执行此操作。来自the manual

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

    由于此功能被禁用file_get_contents() 返回false,这会导致您的第二个错误。

    【讨论】:

      【解决方案2】:

      服务器可能基于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


      注意:

      我可以确认服务器正在根据用户代理拒绝您的请求。

      【讨论】:

      • 仍然没有返回价格。
      • 我已经更新了我的答案,如果对你有帮助,请考虑投票 1+ 并通过单击投票箭头中间的复选标记接受它作为正确答案,tks !
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多