当前位置:网站首页>小程序云开发之--微信公众号文章采集篇
小程序云开发之--微信公众号文章采集篇
2022-07-01 01:10:00 【万神.】
小程序云开发之–微信公众号文章采集篇
相信不少小伙伴都有想过做一个属于自己的小程序,又苦于没有服务器以及备案域名、网站ssl证书等,微信小程序作为前端
后端就有很多了诸如Spring全家桶
等小程序能接收到后端返回的值需要小程序内添加合法域名(域名备案
和https协议
)
这里直接进入教学
公众号中有专门的接口(也有专门的文档) 那么如何采集小程序的文章来做到为我所用呢?
第一步: 获取access_token
通过文档可以看出 想要获取这个access_token需要以下三个参数
其中grant_type
的值为client_credential
用于获取access_token
appid
和secret
在公众号内可以找到
完成以上配置之后就可以采集文章了,
我们可以手动访问获取token或者postman
等
有了token之后采集文章就很方便了 下面开始操作
从官方文档中可以看到提供的接口还是挺多的
这里拿草稿箱做示范吧 其他的像图文、视频等素材的采集方法都是一样的
第一个token我们已经有了接下来就是offset
和count
了 最后一个参数可以不要
获取文章数据
获取到素材之后 打印结果
说明我的草稿箱里有三条数据 确实只有三条
文章数据的处理
我们需要把自己需要的参数获取然后添加进数据库
注意️
这里有个问题就是如果这个文章已经采集过了那么我们就跳过,如果全部数据都存在那么打印 文章已经存在
最后就是把数据库没有的数据放入到数据库中
以下是测试结果
如果全部数据都存在
博客小程序:万神资源小栈
放入部分代码:
/** * 获取公众号文章信息 * @param {*} accessToken */
async function getWechatPosts(accessToken, offset, count) {
let url = `https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=${
accessToken}`
var options = {
method: 'POST',
json: true,
uri: url,
body: {
"type": "news",
"offset": offset,
"count": count
}
}
const result = await rp(options)
let rbody = (typeof result === 'object') ? result : JSON.parse(result);
return rbody;
}
/** * 同步文章的小程序码 */
async function syncPostQrCode() {
let configData = await getConfigInfo("syncPostQrCode");
if (configData == null) {
console.info("未获取相应的配置")
return;
}
console.info(configData)
let page = parseInt(configData.value.currentOffset);
let maxCount = parseInt(configData.value.maxSyncCount);
let isContinue = true;
while (isContinue) {
let posts = await db.collection('mini_posts')
.orderBy('timestamp', 'asc')
.skip(page * 10)
.limit(10)
.field({
_id: true,
qrCode: true,
timestamp: true
}).get()
console.info(posts)
if (posts.data.length == 0) {
isContinue = false;
break;
}
for (var index in posts.data) {
if (posts.data[index].qrCode != null) {
continue
}
let scene = 'timestamp=' + posts.data[index].timestamp;
let result = await cloud.openapi.wxacode.getUnlimited({
scene: scene,
page: 'pages/detail/detail'
})
if (result.errCode === 0) {
const upload = await cloud.uploadFile({
cloudPath: posts.data[index]._id + '.png',
fileContent: result.buffer,
})
await db.collection("mini_posts").doc(posts.data[index]._id).update({
data: {
qrCode: upload.fileID
}
});
}
}
if ((page - parseInt(configData.value.currentOffset)) * 10 > maxCount) {
isContinue = false;
}
else {
page++
}
}
let data = {
currentOffset: page - 1, maxSyncCount: 100 }
await db.collection("mini_config").doc(configData._id).update({
data: {
value: data
}
});
}
以上就是所有内容了 更多精彩内容请关注我~
边栏推荐
- 1175. Prime Arrangements
- Open3d point cloud color rendering
- 测试必备工具—Postman实战教程
- Thinking about business and investment
- 短视频平台开发,依靠DrawerLayout实现侧滑菜单效果
- More pragmatic in business
- 一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!
- New opportunities for vr/ar brought by metauniverse
- qt5-MVC:数据可视化的层次揭秘
- 3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明
猜你喜欢
亲测有效,快速创建JMeter桌面快捷方式
工作八年的程序员,却拿着毕业三年的工资,再不开窍就真晚了...
Gin configuration file
Digital IC design process summary
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
测试必备工具-Postman实战教程
迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况
微生物健康,食品微生物检测为什么很重要
What will Web3 bring in the future?
短信在企业中的应用有哪些?
随机推荐
Unknown database连接数据库错误
Neo4j installation, operation, project construction and function realization
45 year old programmer tells you: why do programmers want to change jobs? It's too true
【栈】921. Minimum Add to Make Parentheses Valid
Zero of DC learning notes -- overview and basic process introduction
[dynamic planning] path dp:931 Minimum Falling Path Sum
【多源bfs】934. Shortest Bridge
Strictmode analysis activity leakage -strictmode principle (3)
【qt5-tab标签精讲】Tab标签及内容分层解析
Thinking about business and investment
3500 word summary: a complete set of skills that a qualified software testing engineer needs to master
[simulation] 922 Sort Array By Parity II
Gin configuration file
laravel Carbon 时间处理类使用
软件开发中的上游和下游
Handsontable数据网格组件
Compile and install oh my Zsh
Necessary tools for testing - postman practical tutorial
Use of typora
PHP crawls data through third-party plug-ins