小红书x-s和web_session

开工发现小红书web更新了,目前笔记信息接口: /api/sns/web/v1/feed

file

请求时headers中需要x-s、x-t,cookie中需要有web_session。


web_session

web_session和当前环境绑定,重新注册后浏览器中的ID也会更新。

其主要依赖的参数是webId,类似于设备指纹。根据测试得出,webId只需要随机生成32位字符即可。

然后通过webId去注册web_session。(目前需要aid、gid、gid—sign,可以随机生成)

或者大家自己复制一个替换吧。

参考代码如下:

import requests
import execjs
import random

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",
        "cookie": f"a1=1879883dcb2xqzqkj7hhp1wtwys56f4jl7omndiyd50000102326; webId={register_Id()}; gid=yYWjYYqdyd1jyYWjYYqfSWf3DJCAuATVWxxUlvfyE9EvM6282Ki4VI888y8JqJK84SqKdfJq; gid.sign=IaDqCp2Q97+mPBsh9MZLtE69xs4=",
        "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-b3-traceid":"453f324415a7086f",
        "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"
    }
    session = requests.post(url,data='{}',headers=headers).json()['data']['session']
    return 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()}",  # 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('x_sx_t.js', 'r', encoding='utf-8') as f:
        js = f.read()
    crt = execjs.compile(js)
    data = '{"source_note_id":"%s"}'%source_note_id
    xs_xt = crt.call('sign','/api/sns/web/v1/feed',{"source_note_id":"%s"%source_note_id})
    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())
    feed("63cf8afe000000001f023d49")

X-S

定位方法很多,可以全局搜 "X-s" 。

![file](http://www.lxs

pider.com/wp-content/uploads/2023/03/641b1de7daa79.png)往上找可以发现该段为 sign 方法,function sign(e, t) {}

全部复制到本地,然后根据报错把缺的方法和环境补一下,比如a0_0x4dee00、a0_0x5c27、a0_0x543e等方法,

然后把常用的navigator、location、document、window加上就好了。

该过程中根据具体错误再调试分析, 比如sign方法的 case "6",修改为var vr = window 、在case "7"中可以手动修改为 dr = ur['sNYMU']

整体的代码我贴上来,大家自己看吧。

云盘的python代码没更新,复制上面的就行。

【温馨提示:此处隐藏内容需要付费订阅后才能查看!】

点赞
  1. byc6352说道:
    Google Chrome Windows 10
    取不到web_session了。返回: {'code': 300015, 'success': False, 'msg': '浏览器异常,请尝试关闭/卸载风险插件或重启试试!', 'data': {}} 大佬能否再研究一下
    1. Lx Lx说道:
      Google Chrome Windows 10
      去看新文章呀,x-s不对返回的 {'code': 300015, 'success': False, 'msg': '浏览器异常,请尝试关闭/卸载风险插件或重启试试!', 'data': {}}
  2. Lx Lx说道:
    Google Chrome Windows 10
    新版本x-s : http://www.lxspider.com/?p=324 其他各种参数:http://www.lxspider.com/?p=220
  3. StageMax StageMax说道:
    Google Chrome Windows 10
    大佬您好,x-s 又更新啦 :bugaoxing:
    1. Lx Lx说道:
      Google Chrome Windows 10
      1
    2. Lx Lx说道:
      Google Chrome Windows 10
      新版: http://www.lxspider.com/?p=324
  4. junhan说道:
    Google Chrome Windows 10
    大佬,小红书X-S又更新了,新版的完全找不到头绪,您有空能看看吗
    1. Lx Lx说道:
      Google Chrome Windows 10
      出差了,过两天更新
    2. Lx Lx说道:
      Google Chrome Windows 10
      更新: http://www.lxspider.com/?p=324
  5. 不懂说道:
    Google Chrome Windows 10
    注册过程貌似变得复杂了 :kuanghan:
    1. Lx Lx说道:
      Google Chrome Windows 10
      需要aid 、gid、gidsign
  6. 说道:
    QQbrowser Android 11
    大佬你好,这个可以做到获取ck吗
    1. Lx Lx说道:
      Google Chrome Windows 10
      现在web_session需要自己的aid、gid、gidsign注册下
      1. het说道:
        Google Chrome Mac OS X 10.15.7
        请问一下,我随机注册了aid、gid、gidsign这三个后,传进cookie,请求这个接口https://edith.xiaohongshu.com/api/sns/web/v1/login/activate生成的session 为啥无效
        1. Lx Lx说道:
          Google Chrome Windows 10
          先看下session是否是正常返回的,如果能取到session,再测接口。 如果接口返回的内容是登录已过期,说明web_session有问题,如果返回code=-1则是请求参数问题。
          1. fw说道:
            Google Chrome Mac OS X 10.15.7
            aid、gid、gid.sign这三个后,传进cookie, 请求:https://edith.xiaohongshu.com/api/sns/web/v1/login/activate 返回成功 但是这个用这个web_session去请求feed接口返回:msg: "无登录信息,或登录信息为空" web_session还需要再次注册吗?还是说gid需要特殊处理注册?
          2. Lx Lx说道:
            Google Chrome Windows 10
            这应该是 web_session 注册有问题,你先把cookie补齐去测试注册。
  7. Lx Lx说道:
    Google Chrome Windows 10
    作品列表: sign("/api/sns/web/v1/user_posted?num=30&cursor=&user_id=54cef5662e1d935ac4b3f554",undefined)

发表回复