delphi/python 实现小红书xhs用户作品列表和图片/视频无水印解析

本文主要是介绍delphi/python 实现小红书xhs用户作品列表和图片/视频无水印解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

技术学习,请勿用与非法用途!!!

成品图

用户作品列表接口
/api/sns/web/v1/user_posted?num=30&cursor=&user_id=642bf0850000000011022c4e&image_scenes=
http Get方式,请求头需要带上x-s x-t签名验证

笔记明细接口
/api/sns/web/v1/feed
data = {"source_note_id":"64356527000000001303282b", "image_scenes":["CRD_PRV_WEBP","CRD_WM_WEBP"]}
http Post方式,请求头需要带上x-s x-t签名验证
x-s算法部分是js+python完成

unit ApiXHS;interface
usesWindows, Messages, SysUtils, Classes, Forms, OverbyteIcsWSocket,OverbyteIcsHttpProt, OverbyteIcsSuperObject, OverbyteIcsUtils, OverbyteIcsWndControl;type// 笔记信息PNoteItem = ^TNoteItem;TNoteItem = recorduser_id: string; //用户idnickname: string; //昵称avatar: string; //头像note_id: string;collected_count: Integer; //收藏数liked_count: Integer; //点赞数comment_count: Integer; //评论数share_count: Integer; //分享数create_time: TDateTime; //创建时间note_type: string; //类型   video -- 或 普通 normaltitle: string; //标题desc: string;image_list: TStrings; //图片列表或视频封面video_addr: string; //视频地址tag_list: TStrings;end;type// 小红书接口--仅学习交流 请勿用于非法用途TXHS = classprivatefunction GetCookie: string;procedure SetCookie(const Value: string);protectedhttp: TSslHttpCli;ssl: TSslContext;_a1: string;publicconstructor Create;destructor Destroy; override;function get_xs( 算法\/: jeomoo168 ): Boolean;function request(url, api, data: string; var json: ISuperObject; var s: string): Boolean; //请求function note_info(p: PNoteItem; var s: string): Boolean; //笔记信息function parse_noteId(url: string): string; //笔记链接->解析对于的笔记idfunction parse_userId(url: string): string; //作者主页链接->解析对于的用户idfunction user_posted(user_id: string;var cursor, s: string; var has_more: Boolean; items: TList): Boolean; //获取作者作品列表property cookie: string read GetCookie write SetCookie;property a1: string read _a1 write _a1;end;procedure InitProfileItem(pi: PProfileItem);
procedure InitNoteItem(p: PNoteItem);implementation
usesShellAPI;procedure InitProfileItem(pi: PProfileItem);
beginpi.user_id := '';pi.nickname := '';pi.avatar := '';pi.desc := '';pi.ipLocation := '';pi.follows := 0;pi.fans := 0;pi.interaction := 0;pi.gender := -1;
end;procedure InitNoteItem(p: PNoteItem);
beginif p = nil then exit;p.user_id := '';p.nickname := '';p.avatar := '';//  p.note_id := '';p.collected_count := 0;p.liked_count := 0;p.comment_count := 0;p.share_count := 0;p.create_time := Now;p.note_type := '';p.title := '';p.desc := '';p.video_addr := '';p.image_list := TStringList.Create;p.tag_list := TStringList.Create;
end;//******************************************************************************
{ TXHS }constructor TXHS.Create;
begininherited;_a1 := '';ssl := TSslContext.Create(nil);http := TSslHttpCli.Create(nil);http.SslContext := ssl;http.Agent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188';http.Accept := 'application/json, text/plain, */*';http.AcceptLanguage := 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6';http.Reference := 'https://www.xiaohongshu.com';http.ContentTypePost := 'application/json;charset=UTF-8'; http.Timeout := 30; //默认30秒
end;destructor TXHS.Destroy;
beginssl.Free;http.Free;inherited;
end;function TXHS.GetCookie: string;
beginResult := http.Cookie;
end;procedure TXHS.SetCookie(const Value: string);
vark: Integer;
beginhttp.Cookie := Value;k := 1;_a1 := ParseTag(Value, 'a1=', ';', k);
end;function TXHS.get_xs(api, data: string; var xs, xt, s: string): Boolean;
varjson: ISuperObject;
beginResult := False;s := '';if a1 = '' then exit; //未设置cookieResult := js-get-xs('x-s生成算法\/: jeomoo168', s);tryjson := SO(s);exceptjson := SO('{}');end;xs := json.S['X-s'];xt := json.S['X-t'];
end;function TXHS.request(url, api, data: string; var json: ISuperObject; var s: string): Boolean;
varxs, xt: string; // , vbuf: UTF8String;
beginResult := False;json := SO('{}');s := '';xs := '';xt := '';data := StringReplace(data, ' ', '', [rfReplaceAll]); //data字符串中不要有空格!!! 不然会请求失败if api <> '' then //api不为空时 需要x-s x-tbeginResult := get_xs(api, EscapeChars(data), xs, xt, s);if not Result then exit;end;//  xs:=''; xt:='';http.URL := url + api;http.ExtraHeaders.Clear;//http.ExtraHeaders.Add('Accept-Encoding: gzip, deflate'); //!!! 不能加这个 加了 返回数据被压缩了http.ExtraHeaders.Add('authority: edith.xiaohongshu.com');http.ExtraHeaders.Add('origin: https://www.xiaohongshu.com');http.ExtraHeaders.Add('Content-Type: application/json;charset=UTF-8');http.ExtraHeaders.Add('x-s: ' + xs);http.ExtraHeaders.Add('x-t: ' + xt);//http.ExtraHeaders.Add('x-s-common: ');http.RcvdStream := TMemoryStream.Create; // For answertryif data = '' thenhttp.Getelsebegin//j2 := SO(data);//v := j2.S['keyword']; //搜索关键词//if v <> '' then//begin//  v := utf8encode(v); //关键词utf8编码// data := Format('{"keyword":"%s","note_type":0,"page":1,"page_size":20,"search_id":"2ci5066wl1gu6kr4q9apa","sort":"general","image_scenes":"FD_PRV_WEBP,FD_WM_WEBP"}',          [v]);//end;data := utf8encode(data);http.SendStream := TMemoryStream.Create;http.SendStream.Write(PChar(data)^, Length(data));http.SendStream.Position := 0; // Send from start!http.Post;end;excepton e: Exception dobegins := Format('%s', [Trim(e.Message)]);exit;end;end;if http.StatusCode <> 200 thenif not SameText(http.RequestDoneErrorStr, 'No Error') thenbegins := http.RequestDoneErrorStr;exit;end;SetLength(buf, http.RcvdStream.Size);Move((http.RcvdStream as TMemoryStream).Memory^, buf[1], http.RcvdStream.Size);http.RcvdStream.Free;http.RcvdStream := nil;if http.SendStream <> nil thenbeginhttp.SendStream.Free;http.SendStream := nil;end;s := Trim(Utf8ToStringA(buf)); //utf8解码   Utf8Decodetryjson := SO(s);Result := json.B['success'];exceptjson := SO('{}');end;//  if Pos('Not Acceptable',s)>0 then     s:=s+Format('  xs=%s',[xs]);
end;function TXHS.note_info(p: PNoteItem; var s: string): Boolean;
varurl, api, data: string;json, info, vi: ISuperObject;va: TSuperArray;i: Integer;
beginResult := False;s := '';if p = nil then exit;InitNoteItem(p);url := 'https://edith.xiaohongshu.com';api := '/api/sns/web/v1/feed';data := '{"source_note_id":"' + p.note_id + '","image_scenes":["CRD_PRV_WEBP","CRD_WM_WEBP"]}';Result := request(url, api, data, json, s); //'Not Acceptable'if not Result then exit;info := json.O['data'].A['items'][0].O['note_card'];if info=nil then exit; //解析失败 有可能接口更新了p.user_id := info.O['user'].S['user_id'];p.nickname := info.O['user'].S['nickname'];p.avatar := info.O['user'].S['avatar'];p.collected_count := info.O['interact_info'].I['collected_count'];p.comment_count := info.O['interact_info'].I['comment_count'];p.share_count := info.O['interact_info'].I['share_count'];p.liked_count := info.O['interact_info'].I['liked_count'];p.desc := info.S['desc'];p.create_time := GetTime_DateTime(info.I['time']);p.title := info.S['title'];p.note_type := info.S['type'];// image_listva := info.A['image_list'];for i := 0 to va.Length - 1 dobeginvi := va[i];p.image_list.Add(vi.A['info_list'][1].S['url']);end;// tag_listva := info.A['tag_list'];for i := 0 to va.Length - 1 dobeginvi := va[i];p.tag_list.Add(vi.S['name']);end;if p.note_type = 'video' thenp.video_addr := 'https://sns-video-bd.xhscdn.com/' + info.O['video'].O['consumer'].S['origin_video_key'];
end;// 笔记链接->解析对于的笔记id
function TXHS.parse_noteId(url: string): string;
vars: string;json: ISuperObject;k: Integer;
beginResult := '';if url = '' then exit;if Pos('xiaohongshu.com/explore/', url) > 0 thenbegink := Pos('?', url);if k > 0 thenurl := Copy(url, 1, k - 1);url := url + '/';k := 1;Result := ParseTag(url, '/explore/', '/', k);exit;end;// http://xhslink.com/xxxxx 格式tryhttp.FollowRelocation := False;request(url, '', '', json, s);finallyhttp.FollowRelocation := True;end;//<a href="https://www.xiaohongshu.com/discovery/item/6558d9300000000032xxxxx?app_platform=ios&amp;app_version=8.14.3&amp;share_from_user_hidden=true&amp;type=normal&amp;xhsshare=CopyLink&amp;appuid=5beaa1f00ac0a40001f2d248&amp;apptime=1700968043">Temporary Redirect</a>.//或//Redirecting to <a href="/discovery/item/6558d930000000003200698c">/discovery/item/6558d9300000000032xxxxx</a>.k := Pos('?', s);if k > 0 thens := Copy(s, 1, k - 1) + '"';k := 1;Result := ParseTag(s, 'discovery/item/', '"', k);
end;// 作者主页链接->解析对于的用户id
function TXHS.parse_userId(url: string): string;
vark: Integer;
begin
// https://www.xiaohongshu.com/user/profile/5565692bb7ba2219xxxxx
// https://www.xiaohongshu.com/user/profile/5565692bb7ba221xxxxxx?xhsshare=CopyLink&appuid=5beaa1f00ac0a40001f2d248&apptime=1700990774Result := '';if url = '' then exit;if Pos('xiaohongshu.com/user/profile/', url) = 0 then exit;k := Pos('?', url);if k > 0 thenurl := Copy(url, 1, k - 1);url := url + '/';k := 1;Result := ParseTag(url, '/profile/', '/', k);exit;
end;//获取作者作品列表
function TXHS.user_posted(user_id: string; var cursor, s: string; var has_more: Boolean; items: TList): Boolean;
varapi, data, url: string;json, vi: ISuperObject;va: TSuperArray;p: PNoteItem;i: Integer;
beginitems.Clear;has_more := False;url := 'https://edith.xiaohongshu.com';api := Format('/api/sns/web/v1/user_posted?num=30&cursor=%s&user_id=%s&image_scenes=',[cursor, user_id]);data := '';Result := request(url, api, data, json, s);if not Result then exit;if json.O['data'] = nil then exit;cursor := json.O['data'].S['cursor'];has_more := json.O['data'].B['has_more'];va := json.O['data'].A['notes'];for i := 0 to va.Length - 1 dobeginApplication.ProcessMessages;vi := va[i];New(p);InitNoteItem(p);Items.Add(p);p.user_id := vi.O['user'].S['user_id'];p.nickname := vi.O['user'].S['nickname'];p.avatar := vi.O['user'].S['avatar'];p.liked_count := vi.O['interact_info'].I['liked_count'];p.note_id := vi.S['note_id'];p.title := vi.S['display_title'];p.note_type := vi.S['type'];end;
end;end.

这篇关于delphi/python 实现小红书xhs用户作品列表和图片/视频无水印解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/471297

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

基于Linux的ffmpeg python的关键帧抽取

《基于Linux的ffmpegpython的关键帧抽取》本文主要介绍了基于Linux的ffmpegpython的关键帧抽取,实现以按帧或时间间隔抽取关键帧,文中通过示例代码介绍的非常详细,对大家的学... 目录1.FFmpeg的环境配置1) 创建一个虚拟环境envjavascript2) ffmpeg-py

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三