用file_get_contents抓取网页乱码的2种解决方式
来源:互联网 发布:软件研发过程模型 编辑:程序博客网 时间:2024/05/20 00:15
今天自己在写一个程序,抓取别人的网页,之前公司有些功能也会需要,但是今天在抓取网页的时候发现了一个问题用file_get_contents抓取网页发现如截图所示的乱码情况
于是用转换编码
$contents = iconv("gb2312", "utf-8//IGNORE",$contents);之前遇到就算乱码也是网页编码的问题,html标签页不会有问题,问题还是没有解决
于是网上找了一下
原因:说是获取的头部信息当中有Content-Encoding: gzip说明内容是GZIP压缩的
然后我试着抓取了自己的博客,发现可以正常抓取,头部信息中也带有Content-Encoding: gzip,为什么会这样就不清楚了,之后在解决下面我推荐2种解决方法:
①、服务器安装zlib库
$contents = file_get_contents("compress.zlib://".$url);②. 使用CURL代替file_get_contents
function curl_get($url, $gzip=false){ $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); if($gzip) curl_setopt($curl, CURLOPT_ENCODING, "gzip"); // 关键在这里 $content = curl_exec($curl); curl_close($curl); return $content;}自此问题解决了,继续下面的程序
QQ交流群:136351212
查看原文:http://www.phpsong.com/1404.html
0 0
- 用file_get_contents抓取网页乱码的2种解决方式
- 用file_get_contents抓取网页乱码的2种解决方式
- file_get_contents抓取网页乱码的解决
- 抓取网页保存时候的乱码解决
- 抓取网页内容,用file_get_contents函数
- file_get_contents网页抓取
- file_get_contents() 函数抓取网页
- Java网页抓取网页内容时,乱码问题的解决
- Java抓取网页乱码解决
- php file_get_contents抓取内容乱码
- PHP中用file_get_contents抓取网页
- 【自己总结的】网页乱码解决方式
- 网页抓取三种方式
- file_get_contents与simple_load_file的时候乱码怎么解决
- Okhttp获取中文网页乱码的解决方式
- VB.Net抓取网页乱码的解决方法
- java抓取网页保存的乱码问题解决
- HttpClient4抓取网页返回乱码的解决办法
- android系统架构-郭霖老师的第一行代码总结
- 对索引存储与散列存储的一些形而上的思考
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- Android之 Animations动画
- 级联菜单(2个tableView共用一个数据源的方式)
- 用file_get_contents抓取网页乱码的2种解决方式
- poj 2886 Who Gets the Most Candies?(线段树+反素数)
- 广度优先搜索的学习
- 业务中多个人工作间的协作思考
- 103. Binary Tree Zigzag Level Order Traversal
- 258. Add Digits LeetCode
- Hibernate配置文件详解
- Codeforces 618 E . Robot Arm
- 104. Maximum Depth of Binary Tree LeetCode