[乐意黎]phpQuery采集微信公众号文章乱码
终于找到解决方案了,这是一个值得庆祝的事情....
原来是因为微信在源码中加入了防采集代码<!--headTrap<body></body><head></head><html></html>-->,把文章源码中的这一段去掉就可以了!
具体代码如下:
public function getCon(){
header("Content-type: text/html; charset=utf-8");
import("Vendor.QL.QueryList");
$w_url=$_POST["wurl"]; //接收到的文章地址
// 测试文章地址
// $w_url="http://mp.weixin.qq.com/s?__biz=MzA5NzQ5OTMxMA==&mid=2650621512&idx=1&sn=2059946e820805c0d62a450aa3af62be&chksm=88960789bfe18e9f47417eb45cd8efe458af9e93fea3e8e4e242ea2376fd3e4c69f5218293cb&scene=0#wechat_redirect";
// echo "<script>alert("".$w_url."");</script>";
$html = file_get_contents($w_url); //获取文章源码并保存到参数中
// echo "<script>alert("".$html."");</script>";
$html = str_replace("<!--headTrap<body></body><head></head><html></html>-->", "", $html); //去除微信中的抓取干扰代码
// die($w_url);
// var_dump($html);
$data = QueryList::Query($html,array(
//采集规则库
//"规则名" => array("jQuery选择器","要采集的属性"),
"titleTag" => array("title","text"),
// "title" => array("#activity-name","text"),
"content" => array("body","text"),
// "image" => array("img","src"),
//微信规则
"contentWx" => array("#js_content","text"),
// "imageWx" => array("img","data-src"),
// "conText" => array(".rich_media_content>p","text"),
))->data;
foreach ($data as $k => $v) {
$data[$k]["imageWx"] = $this->cut_str($v["imageWx"],"?",0);
}
//打印结果
// print_r($data);
$this->assign("conD",$data);
$this->display();
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: PHP采集利器:phpQuery,像jQuery一样轻松采集内容
- 下一篇: java解析json数组
copyright © 2008-2019 入门客AI创业平台 版权所有 备案号:湘ICP备2023012770号