PHP采集公众号文章的绝佳方法,让你轻松获取大量内容!

优采云 发布时间: 2023-03-31 08:17

  随着互联网的不断发展,人们对于信息的获取需求也越来越高。在这个信息爆炸的时代,公众号成为了一个不可或缺的信息来源。但是,手动打开每个公众号、一个一个地查看文章显然是一件费时费力的事情。那么,有没有一种方法可以快速地抓取公众号文章呢?本文将介绍一种基于PHP的公众号文章采集方法。

  一、准备工作

  在开始之前,我们需要准备好以下工具:

  1. PHP环境(建议使用PHP 7.0及以上版本)

  2.网络爬虫框架Goutte(可以通过Composer安装)

  3.公众号账号和密码(用于模拟登录)

  4.公众号名称(用于指定要抓取的公众号)

  二、登录公众号

  

  首先,我们需要使用模拟登录来获取到微信公众平台的Cookie和Token。这里我们可以借助Goutte框架来实现。

  代码如下:

  php

<?php

require_once __DIR__.'/vendor/autoload.php';

use Goutte\Client;

$client = new Client();

$crawler =$client->request('GET','https://mp.weixin.qq.com/');

$form =$crawler->selectButton('登录')->form();

$form['account']='你的账号';

$form['password']='你的密码';

$crawler =$client->submit($form);

$cookie ='';

foreach ($client->getCookieJar()->all() as $cookie){

$cookie .=$cookie->getName().'='.$cookie->getValue().';';

}

$token ='';

preg_match('/token=(\d+)/',$crawler->html(),$9c28d32df234037773be184dbdafc274);

if (isset($9c28d32df234037773be184dbdafc274[1])){

$token =$9c28d32df234037773be184dbdafc274[1];

}

echo 'Cookie:'.$cookie ."\n";

echo 'Token:'.$token ."\n";

  在上面的代码中,我们首先使用Goutte框架模拟打开微信公众平台登录页面,然后填入账号和密码,并提交表单。接着,我们通过遍历CookieJar获取到所有的Cookie,将其拼接成字符串形式。最后,我们使用正则表达式从页面源码中提取出Token。

  三、抓取公众号文章

  有了Cookie和Token之后,我们就可以开始抓取公众号文章了。这里我们还是使用Goutte框架来实现。

  代码如下:

  php

<?php

require_once __DIR__.'/vendor/autoload.php';

use Goutte\Client;

$client = new Client();

$client->setHeader('Cookie','你的Cookie');

$client->request('GET','https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=公众号名称&scene=124#wechat_redirect');

$token ='你的Token';

$url ="https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=公众号名称&f=json&offset=0&count=10&is_ok=1&scene=124&uin=777&key=777&pass_ticket=&wxtoken=&appmsg_token={$token}&x5=0&f=json";

$response =$client->request('GET',$url);

$json =$response->html();

$data = json_decode($json, true);

$articles =[];

if (isset($8d777f385d3dfec8815d20f7496026dc['general_msg_list'])){

$list = json_decode($8d777f385d3dfec8815d20f7496026dc['general_msg_list'], true);

foreach ($list['list'] as $item){

if (isset($item['app_msg_ext_info']['title'])){

$articles[]=[

'title'=>$item['app_msg_ext_info']['title'],

'url'=>'https://mp.weixin.qq.com'.$item['app_msg_ext_info']['content_url'],

'datetime'=> date('Y-m-d H:i:s',$item['comm_msg_info']['datetime']),

];

}

if (isset($item['app_msg_ext_info']['multi_app_msg_item_list'])&& is_f1f713c9e000f5d3f280adbd124df4f5($item['app_msg_ext_info']['multi_app_msg_item_list'])){

foreach ($item['app_msg_ext_info']['multi_app_msg_item_list'] as $subItem){

if (isset($subItem['title'])){

$articles[]=[

'title'=>$subItem['title'],

'url'=>'https://mp.weixin.qq.com'.$subItem['content_url'],

'datetime'=> date('Y-m-d H:i:s',$item['comm_msg_info']['datetime']),

];

}

}

}

}

}

print_r($articles);

  

  在上面的代码中,我们首先使用Goutte框架设置Cookie,然后模拟打开公众号主页。接着,我们构造一个URL,用于获取公众号文章列表。注意,这里的公众号名称和Token需要替换成我们自己的。最后,我们发送HTTP请求,并解析返回的JSON数据,提取出文章的标题、URL和发布时间。

  四、保存文章

  抓取到文章之后,我们可以将其保存到数据库或者文件中。

  代码如下:

  php

<?php

require_once __DIR__.'/vendor/autoload.php';

use Goutte\Client;

$client = new Client();

$client->setHeader('Cookie','你的Cookie');

$client->request('GET','https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=公众号名称&scene=124#wechat_redirect');

$token ='你的Token';

$url ="https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=公众号名称&f=json&offset=0&count=10&is_ok=1&scene=124&uin=777&key=777&pass_ticket=&wxtoken=&appmsg_token={$token}&x5=0&f=json";

$response =$client->request('GET',$url);

$json =$response->html();

$data = json_decode($json, true);

$articles =[];

if (isset($8d777f385d3dfec8815d20f7496026dc['general_msg_list'])){

$list = json_decode($8d777f385d3dfec8815d20f7496026dc['general_msg_list'], true);

foreach ($list['list'] as $item){

if (isset($item['app_msg_ext_info']['title'])){

$articles[]=[

'title'=>$item['app_msg_ext_info']['title'],

'url'=>'https://mp.weixin.qq.com'.$item['app_msg_ext_info']['content_url'],

'datetime'=> date('Y-m-d H:i:s',$item['comm_msg_info']['datetime']),

];

}

if (isset($item['app_msg_ext_info']['multi_app_msg_item_list'])&& is_f1f713c9e000f5d3f280adbd124df4f5($item['app_msg_ext_info']['multi_app_msg_item_list'])){

foreach ($item['app_msg_ext_info']['multi_app_msg_item_list'] as $subItem){

if (isset($subItem['title'])){

$articles[]=[

'title'=>$subItem['title'],

'url'=>'https://mp.weixin.qq.com'.$subItem['content_url'],

'datetime'=> date('Y-m-d H:i:s',$item['comm_msg_info']['datetime']),

];

}

}

}

}

}

//保存到文件

file_put_contents('articles.json', json_encode($articles, JSON_UNESCAPED_UNICODE));

//保存到数据库

$pdo = new PDO('mysql:host=localhost;dbname=test','root','');

foreach ($articles as $article){

$stmt =$pdo->prepare("INSERT INTO articles (title, url, datetime) VALUES (:title,:url,:datetime)");

$stmt->bindParam(':title',$article['title']);

$stmt->bindParam(':url',$article['url']);

$stmt->bindParam(':datetime',$article['datetime']);

$stmt->execute();

}

  在上面的代码中,我们将抓取到的文章保存到了一个JSON文件和MySQL数据库中。

  五、如何处理反爬虫?

  当然,如果你直接按照上面的方法去抓取公众号文章,很可能会遇到反爬虫的问题。这里有一些应对措施,供大家参考。

  

  1.使用代理IP。可以通过购买代理IP或者使用免费的代理IP池来解决IP被屏蔽的问题。

  2.限制请求频率。可以通过设置请求间隔时间或者使用分布式爬虫来减轻服务器压力。

  3.模拟人工操作。可以通过模拟鼠标点击、滑动等操作,来让服务器认为是真实用户在操作。

  六、优采云

  上面我们介绍了如何使用PHP采集公众号文章,并且提到了反爬虫的问题。如果你不想自己去搞这些东西,那么可以考虑使用优采云提供的服务。

  优采云是一家专注于网络数据采集与处理的公司,提供了包括公众号文章采集、微博数据采集、淘宝商品数据采集等在内的各种数据采集服务。不仅如此,优采云还提供了SEO优化、网站建设等相关服务,让您的网站更加完善和专业。

  如果您需要进行数据采集或者其他相关服务,请访问优采云官网:www.ucaiyun.com

  七、总结

  本文介绍了一种基于PHP的公众号文章采集方法,并提供了保存文章、反爬虫等相关技巧。同时,我们还介绍了优采云提供的数据采集和SEO优化服务,希望对大家有所帮助。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线