【问题标题】:How to get the html source of a page correct?如何正确获取页面的html源?
【发布时间】:2014-03-09 16:23:51
【问题描述】:

我使用此代码获取 HTML 源代码:

    <?php
header('Content-Type: text/html; charset=utf-8');
$html = file_get_html("http://www.google.com/");
echo $html;

但是,当我想从 here 获取源代码时,我的响应不正确,我得到了类似以下字符的内容:

���moY�&�9����i�[S$%ٲ�9������l�l/���F"H�H�VDPJ����˲59��[��v���R�Vɖ3KY��_A����_�  ��so�1�N��T�E"#nܸ��s��=�  ��������?�?�������  ���|������0Vk��Z�2o��E۪  ү�XF�ny���;v�R�ܦ���F�Ƨe˷  ��g����{�������}

【问题讨论】:

  • utf-8 是该网站上语言的正确编码吗?
  • 服务器返回可能是压缩后的数据,试试这个:php.net/manual/en/function.gzdecode.php
  • 使用 cURL 下载内容并使用str_get_html

标签: php


【解决方案1】:

默认情况下,来自 Google 的内容使用某种 HTTP 压缩。两种常用的压缩模式是gzipdeflate。在此处阅读更多信息:

http://en.m.wikipedia.org/wiki/HTTP_compression

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多