首页 > 编程语言 >python获取小红书web_session,以及解决x-s签名验证(2023-07-09)

python获取小红书web_session,以及解决x-s签名验证(2023-07-09)

时间:2023-07-09 14:31:54浏览次数:52  
标签:web 07 09 js session sec xs xt

一、web_session请求接口:

https://edith.xiaohongshu.com/api/sns/web/v1/login/activate

请求类型:

post

提交数据:

{}

这儿是两个字符{},笔者最初提交None,总得不到结果,chrome F12才发现需要这两个字符。


二、签名验证x-s

 该请求需要x-s签名验证,签名代码如下:

    a1 = "186d30820a4m09cb6glhxe1aqks2olv1l97ow1gun50000408882"
    data = '{}'
    api='/api/sns/web/v1/login/activate'
    #需要xs.js签名文件的+~~~v
    with open('xs.js', 'r', encoding='utf-8') as f:
        js = f.read()
    crt = execjs.compile(js)
    xs_xt = crt.call('get_xs','/api/sns/web/v1/login/activate',data,a1)
    xs_xt['X-t'] = str(xs_xt['X-t'])

三、完整python源码(有详细注释)

import requests
import execjs
import random
import json

#生成webId
def register_Id(c=32):
    s = "abcdef0123456789"
    webId = ''
    for i in range(c):
        webId += random.choice(s)
    return webId


def register_session():
    url = 'https://edith.xiaohongshu.com/api/sns/web/v1/login/activate'  #请求接口
    headers = {
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7",
        "cache-control": "no-cache",
        "content-type": "application/json;charset=UTF-8",
        "Content-Length":'2',
         #cookie中,这些内容是必须的。注意a1要和签名的a1一致。
        "cookie": f"webBuild=2.14.4; xsecappid=xhs-pc-web;a1=186d30820a4m09cb6glhxe1aqks2olv1l97ow1gun50000408882; webId={register_Id()}; gid=yYKfq8YJW41JyYKfq8YJ8jyk0468jSDKk1xCW7Idy0ATMl28Jl1hyE88848YYYJ80diKYiYK; gid.sign=tdAyngUKDoCYVgjPneEo26u7o1w=; websectiga=a9bdcaed0af874f3a1431e94fbea410e8f738542fbb02df1e8e30c29ef3d91ac; sec_poison_id=7af5d9b1-face-4972-9e25-0dbc7977cd34",
        "origin": "https://www.xiaohongshu.com",
        "pragma": "no-cache",
        "referer": "https://www.xiaohongshu.com/",
        "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Google Chrome\";v=\"114\", \"Google Chrome\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-site",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "X-B3-Traceid":"c3a6a5705e92dfe2",
        "X-S-Common": "2UQAPsHC+aIjqArjwjHjNsQhPsHCH0rjNsQhPaHCH0P1PjhIHjIj2eHjwjQgynEDJ74AHjIj2ePjwjQhyoPTqBPT49pjHjIj2ecjwjHUN0Z1+UHVHdWMH0ijP/W7w/WhP9z0G0Qhqgkly9i7yBYIPg4F47SA+/8f+BkV+9RTJfzk2ncMPeZIPerIP0PU+jHVHdW9H0il+0WlweDlPerEPerUNsQh+UHCHDRd4A8A8AzpP/Q7c0mdzfSA8MzgqMSl40bd/FQOcd4sq9lOp/mxqF4A8FGAHjIj2eWjwjQQPAYUaBzdq9k6qB4Q4fpA8b878FSet9RQzLlTcSiM8/+n4MYP8F8LagY/P9Ql4FpUzfpS2BcI8nT1GFbC/L88JdbFyrSiafp/JDMra7pFLDDAa7+8J7QgabmFz7Qjp0mcwp4fanD68p40+fp8qgzELLbILrDA+9p3JpHlLLI3+LSk+d+DJfRAyfRL+gSl4bYlqg48qDQlJFShtUTozBD6qM8FyFShPo+h4g4U+obFyLS3qd4QyaRAy9+0PFSe/B8QPFRSPopFJeHIzbkA/epSzb+t8nkn4AmQynpS2b87/sTc4BRUqgziLrSN8gY8wBRQcMHlaLpUL94n4FQoqgzaagYbGnpr8Bpn4g4xaLPIqAbl4BEQy7mNanYgnfpSP7+88Fq9GMLMqMSl4okYzBzS8dklPgkDN9pgpF8wagWM8n8M4sRQzLES8SmFcgQCqfYOJM8oag8d8nSl4oL6nn4S2BklJLS3/o4QyLzcz9bTyDS9yBF3a/WhanSC4LQn49lQ4D4B+Bp98Lz/+9Ll4gzeaLP7qFz0O/FjNsQhwaHCP/L9Per9+/cANsQhP/Zjw0rIKc==",
        "X-S": "Ogw6sg4U12wB0gFisgTWsYqv1gOBOBwBslOU0jsGsgF3",
        "X-T": "1681891019012"
    }
    
    a1 = "186d30820a4m09cb6glhxe1aqks2olv1l97ow1gun50000408882"    #必须与cookie中的a1一致
    data = '{}'                                                                                                      #数据
    api='/api/sns/web/v1/login/activate'                                                          #短链接
    #调用js签名文件。需要xs.js签名文件的+~~~:   byc6352
    with open('xs20230530.js', 'r', encoding='utf-8') as f:
        js = f.read()
    crt = execjs.compile(js)
    xs_xt = crt.call('get_xs','/api/sns/web/v1/login/activate',data,a1)
    xs_xt['X-t'] = str(xs_xt['X-t'])

    headers["X-S"]=xs_xt["X-s"]
    headers["X-T"]=xs_xt["X-t"]
    #print(headers)

    session = requests.post(url,data='{}',headers=headers).json()['data']['session']
    #session = requests.post(url,data='{}',headers=headers).json()
    print(session)
    return session
    #通过请求笔记详情接口,验证web_session的有效性
def feed(source_note_id):
    headers = {
        "accept":"application/json, text/plain, */*",
        "accept-encoding":"gzip, deflate, br",
        "accept-language":"zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7",
        "cache-control":"no-cache",
        "content-type":"application/json;charset=UTF-8",
        "cookie":f"web_session={register_session()};a1=186d30820a4m09cb6glhxe1aqks2olv1l97ow1gun50000408882",  # web_session和当前IP或者环境绑定,重新注册后浏览器中的ID也会更新
        "origin":"https://www.xiaohongshu.com",
        "pragma":"no-cache",
        "referer":"https://www.xiaohongshu.com/",
        "sec-ch-ua":"\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"",
        "sec-ch-ua-mobile":"?0",
        "sec-ch-ua-platform":"\"Windows\"",
        "sec-fetch-dest":"empty",
        "sec-fetch-mode":"cors",
        "sec-fetch-site":"same-site",
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
        "X-s":"1lqJsBVUOjsiO6dk1BspOg9G16O6sgAWslOJ1gkvOgF3",
        "X-t":"1675387207946"
    }
    with open('xs20230530.js', 'r', encoding='utf-8') as f:
        js = f.read()
    crt = execjs.compile(js)
    data = '{"source_note_id":"%s"}'%source_note_id
    a1 = "186d30820a4m09cb6glhxe1aqks2olv1l97ow1gun50000408882"
    xs_xt = crt.call('get_xs','/api/sns/web/v1/feed',data ,a1)
    xs_xt['X-t'] = str(xs_xt['X-t'])

    headers.update(xs_xt)
    feed = 'https://edith.xiaohongshu.com/api/sns/web/v1/feed'
    print(requests.post(url=feed, data=data, headers=headers).text)

if __name__ == '__main__':
    #print(register_session())
    print("动态生成web_session并使用该web_session获得小红书笔记详情数据。技术支持:v+:byc6352")
    feed("63cf8afe000000001f023d49")

python获取小红书web_session,以及解决x-s签名验证(2023-07-09)_x-s签名算法

标签:web,07,09,js,session,sec,xs,xt
From: https://blog.51cto.com/u_16113078/6668017

相关文章

  • C++电影评分系统[2023-07-09]
    C++电影评分系统[2023-07-09]程序设计综合课程设计任务书任课教师:张启军班级:22数字媒体1、2、重、补修班时间:第20周分组:2人一组(经老师同意后可1人或3人一组)一、题目电影评分系统二、课程设计目的和要求本课程设计通过完成一个规模适当的、完整的程序,综合运用......
  • 英语0707
    1.lookforwardto期望,to是介词,后面跟名词takepartin参加runoutof用完stayawayfrom远离2.worse更糟的,比较级3.belinkedupwith与....连接起来(实物)bedealwith被如何处理takeas被看作betiedupwith与...联系在一起(宏观的)4.co......
  • abc309e <dfs>
    E-FamilyandInsurance//https://atcoder.jp/contests/abc309/tasks/abc309_e//<dfs>//关键在于意识到,每个结点保留最大后代数即可#include<iostream>#include<algorithm>#include<vector>usingnamespacestd;typedeflonglongLL;constintN=3......
  • abc309f <线段树 + 离散化 + 双指针>
    F-BoxinBox//https://atcoder.jp/contests/abc309/tasks/abc309_f//<线段树+离散化+双指针>[unique+lower_bound+erase+lambda+vector]//总体思路:将每个三元组记录为如a[3]的3维向量,依次考虑每个向量,检查是否存在一个向量完全比它'小'//将向量按......
  • LeetCode 207. 课程表
    classSolution{public:boolcanFinish(intn,vector<vector<int>>&pre){if(pre.empty()||pre[0].empty())returntrue;vector<vector<bool>>g(n,vector<bool>(n,false));for(autoq:pre)......
  • 成语积累 20230709
    踔厉奋发:踔:跳动;形容精神振作,意气奋发。近义:踔厉风发,踔厉骏发。例句:我们唯有~,笃行不怠,方能不负历史,不负时代,不负人民。不舞之鹤:不舞蹈的鹤。比喻名不副实的人,或讥讽人无能。例句:他总是夸夸其谈,说自己经历过许多大的场面,可真到了该委以重任之时,又远远的躲到了一边,不过是~。虚室生......
  • C# httpclient获取cookies实现模拟web登录
    目前在公司做一款平台化的产品,我主要负责PC端上的开发,在产品推荐过程中为了节省开发时间很多功能模块没来得及做原生,用CEF嵌入了很多带功能web页面,与客户端进行交互从而实现功能。在二期开发中,产品需求说明书中需要把登录功能放在客户端来做,这当中涉及到一个问题客户端做登入登出......
  • 20230709 - 一句SQL更新WordPress管理员密码
    该方法适用于有wordpress数据库权限,但忘记了管理员密码的情况UPDATEwp_usersSETuser_pass=MD5('new_password')WHEREwp_users.user_login='admin_username';更新时,密码为MD5加密字符串,此时可以使用new_password登录,登录后,WordPress会自动更新密码为新加密字符格式。......
  • P4645 [COCI2006-2007#3] BICIKLI
    P4645[COCI2006-2007#3]BICIKLI题意:求一张\(n\)个点的有向图中\(1\)号点到\(2\)号点的路径数。首先考虑不在\(1\)号点到\(2\)号点的路径上的那些点不会对答案产生影响,于是先预处理出所有\(1\)号点到\(2\)号点路径上经过的点。先在原图上以\(1\)号点为起点对所......
  • AtCoder Beginner Contest 309
    感觉F写了个乱搞做法A-Nine(abc309A)题目大意给定一个\(3\times3\)的网格,以及两个数字。问这两个数字是否水平相邻。解题思路求出两个数字的横纵坐标,看是否横坐标相同,纵坐标差一即可。读题不仔细,开题就WA了。神奇的代码#include<bits/stdc++.h>usingnamespa......