Home  >  Article  >  Backend Development  >  Example of QueryList parsing WeChat articles

Example of QueryList parsing WeChat articles

小云云
小云云Original
2018-03-27 10:46:563945browse

最近工作中需要做一个通过微信文章url抓取微信文章的功能,网页解析使用的是QueryList。将代码实现的逻辑记录一下。希望能帮助到大家。

具体实现代码如下:

/**
     * @param $url  微信文章url
     * @return bool
     */
    function spideWx($url){
        if(empty($url)) return false;
        $_host = parse_url($url, PHP_URL_HOST);  //获取主机名
        if($_host !== 'mp.weixin.qq.com') return false;  //不是来自微信文章
        $html = file_get_contents($url);
        if(empty($html)) return false;
        $html = str_replace("", "", $html);  //去除微信干扰元素!!!否则乱码
        preg_match("/var msg_cdn_url = \".*\"/", $html, $matches);   //获取微信封面图
        $coverImgUrl = $matches[0];
        $coverImgUrl = substr(explode('var msg_cdn_url = "', $coverImgUrl)[1], 0, -1);
        $rules = array(   //设置QueryList的解析规则
            'content' => array('#js_content', 'html'),  //文章内容
            'title' => array('#activity-name', 'text'),  //文章标题
            'author'=> array('.rich_media_meta_text:eq(1)','text'),  //作者
            'account_name' => array('#js_profile_qrcode .profile_nickname','text'),  //公众号
            'account_en_name' => array('#js_profile_qrcode .profile_meta:eq(0) .profile_meta_value','text'),  //公众号英文标识
        );
        //替换图片链接,解决微信图片防盗链
        $_link = 'http://read.html5.qq.com/image?src=forum&q=5&r=0&imgflag=7&imageUrl=';
        $data = QueryList::Query($html,$rules)->getData();   //执行解析
        $_res = $data[0];  //获取解析结果
        if(empty($_res)) return false;  //解析失败
        $_res['thumb'] = $_link.$coverImgUrl;   //封面图
        $_res['title_crc'] = sprintf("%u", crc32($_res['title']));   //标题crc
        $_res['url_crc'] = sprintf("%u", crc32($url));   //url-crc
        $pattern = '/]*)src\s*=\s*([\' "])([\s\S]*?)([^>]*)/';    //正则替换内容中的图片链接
        $_res['content'] = preg_replace($pattern, '

相关推荐:

PHP 用QueryList抓取网页内容

用QueryList采集金山词霸《每日一句》

QueryList最简单的PHP采集工具,以采集百度乐彩彩票开奖号为例

The above is the detailed content of Example of QueryList parsing WeChat articles. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn