QueryList公众号文章采集工具,教你如何轻松获取海量内容!
优采云 发布时间: 2023-04-04 07:10QueryList 是一款强大的 PHP 采集工具,可用于采集各种网页数据。在本文中,我们将介绍如何使用 QueryList 采集公众号文章。
1.安装 QueryList
首先,我们需要在我们的项目中安装 QueryList。可以通过 Composer 进行安装:
composer require jaeger/querylist
2.获取微信公众号文章列表
要获取公众号文章,我们需要先获取公众号文章列表。可以使用以下代码获取文章列表:
php
use QL\QueryList;
$publicAccount ='XXXXX';//公众号名称
$html = file_get_contents("https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz={$publicAccount}&scene=124#wechat_redirect");
$data = QueryList::html($html)
->find('.weui-desktop-mass-appmsg__bd .weui-desktop-mass-appmsg__item')
->map(function ($item){
return [
'title'=>$item->find('.weui-desktop-mass-appmsg__title')->text(),
'link'=>$item->find('.weui-desktop-mass-appmsg__title')->attr('href'),
'cover'=>$item->find('.weui-desktop-mass-appmsg__thumb')->attr('data-src'),
'abstract'=>$item->find('.weui-desktop-mass-appmsg__desc')->text(),
];
})
->toArray();
3.获取文章内容
获取到文章列表后,我们可以使用 QueryList 获取每篇文章的内容。可以使用以下代码获取文章内容:
php
use QL\QueryList;
$url ='https://mp.weixin.qq.com/s/XXXXX';//文章链接
$html = file_get_contents($url);
$data = QueryList::html($html)
->find('#js_content')
->html();
4.保存文章内容
获取到文章内容后,我们可以将其保存到本地文件或数据库中。以下是将文章内容保存到本地文件的代码:
php
file_put_contents('article.html',$data);
5.总结
在本文中,我们介绍了如何使用 QueryList 采集公众号文章。首先,我们获取公众号文章列表,然后使用 QueryList 获取每篇文章的内容,并将其保存到本地文件或数据库中。希望这篇文章能对您有所帮助。