美文网首页
PHP 抓 html 内容乱码

PHP 抓 html 内容乱码

作者: 游城十代2dai | 来源:发表于2018-06-12 10:33 被阅读16次

    两种抓取方式

    • 使用 file_get_contents
    $url = 'http://203.90.137.79/jsxsd/';
    // 服务器安装 zlib 库
    $html = file_get_contents("compress.zlib://".$url);
    
    // 如果使用 zlib 库还是乱码就再用下面的转码
    $html = iconv("gb2312", "utf-8//IGNORE",$html);
    
    • 使用 curl
    $ch = curl_init();  
    $timeout = 10; // set to zero for no timeout  
    curl_setopt ($ch, CURLOPT_URL,'http://203.90.137.79/jsxsd/');  
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
    curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36');  
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
    curl_setopt ($ch, CURLOPT_ENCODING, 'gzip');
    $html = curl_exec($ch);  
    curl_close($ch);
    
    // 同理, 上面的 gzip 无法解决乱码的时候也再加上这句话
    $html = iconv("gb2312", "utf-8//IGNORE",$html);
    
    • PS: Linux 扒网站资源
    wget -r -p -np -k http://www.yj720.cn/tour/c5117fc78cabaa75\?from\=groupmessage\&isappinstalled\=0
    

    相关文章

      网友评论

          本文标题:PHP 抓 html 内容乱码

          本文链接:https://www.haomeiwen.com/subject/zmlleftx.html