【问题标题】:Simple HTML DOM Issue简单的 HTML DOM 问题
【发布时间】:2016-10-21 14:52:30
【问题描述】:

在使用 Simple HTML DOM 库时,我遇到了一些网站的问题。当我尝试加载以下网址时http://www.t-mobile.com/shop/phones/cell-phone-detail.aspx?cell-phone=HTC-One-S-Gradient-Blue&tab=reviews#BVRRWidgetID

我的 PHP 代码是:

<?php

include "simple_html_dom.php";

$html=new simple_html_dom();
$url="http://www.t-mobile.com/shop/phones/cell-phone-detail.aspx?cell-phone=HTC-One-S-    Gradient-Blue&tab=reviews#BVRRWidgetID";
$html->load_file($url);
echo $html;

?>

php脚本没有报错,但每次都显示如下内容。

Unsupported Browser
It appears that you are viewing this page with an unsupported Web browser. This Web site works best with one of these supported browsers:

Microsoft Internet Explorer 5.5 or higher
Netscape Navigator 7.0 or higher
Mozilla Firefox 1.0 or higher

If you continue to view our site with your current browser, certain pages may not display correctly and certain features may not work properly for you.

有什么问题? Simple HTML DOM 有限制吗?有没有其他方法可以解决这个问题?

【问题讨论】:

  • 您的第一个问题是抓取网站。但是让我们忘记这一点,我认为您需要设置一个适当的用户代理

标签: simple-html-dom


【解决方案1】:

某些网站不允许直接废弃其内容。

您可以使用 curl 获取 html 内容,然后使用 dom 对象的 load()。

我希望它对你有用。

【讨论】:

    【解决方案2】:

    只需在 simple_html_dom 请求中设置您的 USERAGENT:

    # Creating useragent array
    $useragent = array("http" => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6");
    
    # Creating a line from array
    $useragent = stream_context_create($useragent);
    
    # Starting Simple_HTML_Dom with our useragent
    $html = file_get_html($urlCategory, $useragent)
    

    因此,我们的请求将来自比您更新的浏览器。

    【讨论】:

      【解决方案3】:

      设置用户代理

      $context = stream();
      stream($context, array('user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n'));
      file_get_html('http://www.t-mobile.com/shop/phones/cell-phone-detail.aspx?cell-phone=HTC-One-S-    Gradient-Blue&tab=reviews#BVRRWidgetID', 0, $context);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-27
        • 1970-01-01
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        相关资源
        最近更新 更多