当前位置: 首页 > news >正文

图集短视频去水印云函数开发实践——小红书

前两篇主要讲解了抖音和快手的图集短视频对去水印解析的云函数开发实践,今天说一些小红书图集解析的云函数实践。

图集短视频去水印云函数开发实践——抖音

图集短视频去水印云函数开发实践——快手

其实都是大差不差的,首先获取到小红书的分享链接,然后重定向到原地址,然后直接请求这个地址,获取到网页HTML,直接从中提取,这里比抖音快手还要简单一些。具体代码如下:

async getRedirectUrl(url) {try {const response = await this.curl(url, {method: "GET",headers: this.headers,followRedirect: false,});return this.safeGet(response, 'headers.location', url);} catch (error) {console.error("获取重定向URL时出错:", error);throw error;}}async getHtml(url) {try {const response = await this.curl(url, {headers: this.headers,dataType: "text",});return this.safeGet(response, 'data', null);} catch (error) {console.error("获取网页内容失败:", error);return null;}}parseHtml(html) {const jsonMatch = html.match(/<script>window\.__INITIAL_STATE__=(.*?)<\/script>/);if (!jsonMatch || jsonMatch.length < 2) {console.error("无法找到笔记信息");return null;}try {let jsonString = jsonMatch[1].replace(/undefined/g, "null");const data = JSON.parse(jsonString);const noteId = Object.keys(this.safeGet(data, 'note.noteDetailMap', {}))[0];if (!noteId) {console.error("无法找到笔记ID");return null;}const noteData = this.safeGet(data, `note.noteDetailMap.${noteId}.note`, null);if (!noteData) {console.error("无法获取笔记数据");return null;}const result = {title: this.safeGet(noteData, 'title', ''),desc: this.safeGet(noteData, 'desc', ''),type: this.safeGet(noteData, 'type', ''),user: {nickname: this.safeGet(noteData, 'user.nickname', ''),avatar: this.safeGet(noteData, 'user.avatar', ''),userId: this.safeGet(noteData, 'user.userId', ''),},time: this.safeGet(noteData, 'time', ''),likes: this.safeGet(noteData, 'interactInfo.likedCount', '0'),comments: this.safeGet(noteData, 'interactInfo.commentCount', '0'),collects: this.safeGet(noteData, 'interactInfo.collectedCount', '0'),view_count: this.safeGet(noteData, 'interactInfo.viewCount', '0'),share_count: this.safeGet(noteData, 'interactInfo.shareCount', '0'),platform: "xiaohongshu",};if (noteData.type === "video") {result.video = {url: this.safeGet(noteData, 'video.media.stream.h264.0.masterUrl', ''),cover: this.safeGet(noteData, 'video.cover.url', ''),};} else {result.images = this.safeGet(noteData, 'imageList', []).map((img) => ({url: this.safeGet(img, 'urlDefault', '') || this.safeGet(img, 'url', ''),width: this.safeGet(img, 'width', 0),height: this.safeGet(img, 'height', 0),}));}return result;} catch (error) {console.error("解析笔记信息失败:", error);return null;}}// 辅助方法:将字符串解析为数字parseNumber(value) {if (typeof value === "number") return value;if (!value) return 0;const num = parseInt(value.replace(/[^0-9]/g, ""));return isNaN(num) ? 0 : num;}safeGet(obj, path, defaultValue = '') {return path.split('.').reduce((acc, part) => {if (acc && typeof acc === 'object' && part in acc) {return acc[part];}return defaultValue;}, obj);}

没那么多废话了,看代码应该就可以明白了,不明白的留言问就好了。

http://www.lryc.cn/news/470242.html

相关文章:

  • Uni-App-03
  • 解决 VScode 每次打开都是上次打开的文件问题
  • redis高级篇之skiplist跳表 第164节答疑
  • Java 线程池:深入理解与高效应用
  • week08 zookeeper多种安装与pandas数据变换操作-new
  • js构造函数和原型对象,ES6中的class,四种继承方式
  • 电脑连接海康相机并在PictureBox和HWindowControl中分别显示。
  • 直播系统源码技术搭建部署流程及配置步骤
  • Spring+ActiveMQ
  • Linux 常用命令总汇
  • fmql之Linux RTC
  • Flask-SocketIO 简单示例
  • Vue 3 的组件式开发(2)
  • python 爬虫 入门 四、线程,进程,协程
  • cloak斗篷伪装下的独立站
  • 【Nas】X-DOC:在Mac OS X 中使用 WOL 命令唤醒局域网内 PVE 主机
  • u盘装win10系统提示“windows无法安装到这个磁盘,选中的磁盘采用GPT分区形式”解决方法
  • Linux系统之dc计算器工具的基本使用
  • 使用Python计算相对强弱指数(RSI)进阶
  • vue 解决:npm ERR! code ERESOLVE 及 npm ERR! ERESOLVE could not resolve 的方案
  • Android 原生开发与Harmony原生开发浅析
  • VIVO售后真好:屏幕绿线,4年免费换屏
  • 数据类型【MySQL】
  • 流媒体协议.之(RTP,RTCP,RTSP,RTMP,HTTP)(二)
  • 在 Kakarot ZkEVM 上使用 Starknet Scaffold 构建应用
  • DBeave如何连接达梦数据库,设置达梦驱动,真酷
  • 2024年全球 MoonBit 编程创新赛-零基础早鸟教程-使用wasm4八小时开发井子棋小游戏
  • 机器学习4
  • Python数值计算(33)——simpson 3/8积分公式
  • <项目代码>YOLOv8路面垃圾识别<目标检测>