介绍

使用QueryList采集微信公众号文章并自动转为md语法

框架安装

安装可以看之前写的文章:http://blog.oioweb.cn/36.html

实现代码

use QL\QueryList;

$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'https://mp.weixin.qq.com/s?__biz=MzIxMTAyNjk0OA==&mid=2654610821&idx=1&sn=e099b2e3a82ac706d0d5d37475be4541&chksm=8c9510eebbe299f8648384619f3a8fab85feeb276c294a569bc4779fdd16c63c8635a44b263c&scene=132#wechat_redirect');
$html = (string)$res->getBody();
$title  = QueryList::html($html)->find('meta')->map(function ($Row){
    $property = $Row->attr('property');
    if ($property=="og:title"){
        return $Row->content;
    }
});
foreach ($title->all() as $value){
    if (isset($value)){
        $title = $value;
        break;
    }
}
$name = QueryList::html($html)->find("#js_name")->text();
$text = QueryList::html($html)->find("#js_content")->html();
$details = preg_replace_callback('/<img.*?data-src="(.*?)".*?>/is', function ($text) {
    global $title;
    return "\n".'!['.$title.']('.$text[1].')'."\n";
}, $text);
print_r([
   'title'=>$title,
   'name'=>$name,
   'html'=>QueryList::html($details)->find("")->text(),
]);

效果

QueryListPHP框架采集微信公众号文章

最后修改:2020 年 12 月 08 日 01 : 08 PM
如果觉得我的文章对你有用,请随意赞赏