【问题标题】:PHP Simple HTML DOM return gibberish when website enable caching当网站启用缓存时,PHP Simple HTML DOM 返回乱码
【发布时间】:2021-12-12 08:14:49
【问题描述】:
include('/simple_html_dom.php');

$url = "https://www.2311666.com.tw/";
$opts = array(
   'http'=> array('header'=>"User-Agent:Chrome/94.0.4606.81\r\n"));
$context = stream_context_create($opts);
$html = new simple_html_dom();
$get = file_get_contents($url,false,$context);
$html->load($get);

echo $html;

附:以上代码部署在服务器上。

我可以抓取大多数网站,但一些使用 WordPress 内部缓存的特定网站返回乱码。

更准确地说,第一次抓取正常,但第二次抓取显示乱码

我尝试过使用 cURL 或动态用户代理,但得到了相同的结果。有正确的方法吗?

【问题讨论】:

    标签: php web-scraping caching


    【解决方案1】:

    使用 cURL 并添加 curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "") 或者 gzdecode() 函数可以解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 2015-05-09
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      相关资源
      最近更新 更多