采集网址时发现是乱码,但用chrome编码转换后还是乱码,于是确定不是编码问题。
找到原因是,user_agent的问题,需要curl模拟浏览器访问才能得到正常的数据。
function curlx()
{
header("Content-Type:text/html; charset=GBK");
$url = "http://tv.sohu.com/s2013/wstzbzhfh/";
$ch = curl_init();//curl
$user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$str = curl_exec($ch); //得到页面内容
//$str = mb_convert_encoding($output, "utf-8", "GBK");
echo($str);
curl_close($ch);//释放句柄
}
本文介绍了一种解决网页乱码的方法,通过设置正确的User-Agent来获取正常的页面数据,而不是通过编码转换。文中提供了一个PHP脚本示例,展示了如何使用curl模拟浏览器请求。
5237

被折叠的 条评论
为什么被折叠?



