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

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连