虛擬資源站碼支付wordpress全網(wǎng)營銷推廣公司
前兩篇主要講解了抖音和快手的圖集短視頻對去水印解析的云函數(shù)開發(fā)實(shí)踐,今天說一些小紅書圖集解析的云函數(shù)實(shí)踐。
圖集短視頻去水印云函數(shù)開發(fā)實(shí)踐——抖音
圖集短視頻去水印云函數(shù)開發(fā)實(shí)踐——快手
其實(shí)都是大差不差的,首先獲取到小紅書的分享鏈接,然后重定向到原地址,然后直接請求這個(gè)地址,獲取到網(wǎng)頁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時(shí)出錯(cuò):", 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("獲取網(wǎng)頁內(nèi)容失敗:", 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("無法獲取筆記數(shù)據(jù)");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;}}// 輔助方法:將字符串解析為數(shù)字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);}
沒那么多廢話了,看代碼應(yīng)該就可以明白了,不明白的留言問就好了。