成品圖
評論接口
https://edith.xiaohongshu.com/api/sns/web/v2/comment/page?note_id=筆記id&cursor=光標
初次使用cursor為空,該接口為GET,需要x-s,x-t簽名驗證
子評論接口
https://edith.xiaohongshu.com/api/sns/web/v2/comment/sub/page?note_id=%s&root_comment_id=%s&num=10&cursor=%s
x-s x-t簽名生成
# 生成簽名演示代碼...
import sys
import os
import execjsos.environ['execjs_runtime'] = 'node'
api = '/api/sns/web/v2/comment/page?note_id=筆記id&cursor=' #
a1 = '18be78c8854wic46cdkwy1zzrlwq7stzll75ehpsi50000319688' #具體改為自己的cookie里面的a1
data = '' def main():# 定義參數global api #調用全局變量global a1global datajs_code = open(r'./地球:jeomoo168獲取js文件.js','r', encoding='utf-8').read()js = execjs.compile(js_code)try:ret = js.call('get_xs', api, data, a1)except Exception as e:print('缺少nodejs環境')sys.exit(1)if __name__ == '__main__':main()
獲取評論
?
import os
import re
import sys
import time
import execjs
import requestsos.environ['execjs_runtime'] = 'node'def get_headers():return {"authority": "edith.xiaohongshu.com","accept": "application/json, text/plain, */*","accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","content-type": "application/json;charset=UTF-8","origin": "https://www.xiaohongshu.com","referer": "https://www.xiaohongshu.com/","user-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","x-s": "","x-t": ""}cookies_obj ='{"sec_poison_id":"0b4e5e8d-97e5-448d-bc33-1343e6b30851", "webBuild":"3.17.4", "cacheId":"173b5563-69c1-42b9-a15f-bd1d73655eb3", "abRequestId":"f29caa4c-b841-5301-8d62-5166b189e0b6", "web_session":"0400697e396669a2821ea71454374bbbf4ed1e", "websectiga":"3633fe24d49c7dd0eb923edc8205740f10fdb18b25d424d2a2322c6196d2a4ad", "a1":"18c044f0a855kcduza375j9gutezkft2w8lyo56nj50000111235", "gid":"yYS844iyJKEKyYS844i80Y2KY22TSf7u0qW21IVVjk79dv28uTi9Jl888yyJWKy84SSSD12i", "webId":"169a3d7bd4f60a4ff2d504fbe8ca5aea", "xsecappid":"xhs-pc-web"}'
cookies = eval(cookies_obj) # 轉格式json
headers = get_headers()note_id = 筆記id
cursor = ''
api = f"/api/sns/web/v2/comment/page?note_id={note_id}&cursor={cursor}"
a1 = cookies['a1']
data = ''js_code = open('\/:jeomoo168獲取js文件.js', encoding='utf-8').read()
js = execjs.compile(js_code)try:ret = js.call('get_xs', api, data, a1)
except Exception as e:print('缺少nodejs環境')print(e.args)sys.exit(1)xs = ret['X-s']
xt = str(ret['X-t'])
print(f'js-get_xs(): xs={xs}')
print(f'js-get_xs(): xt={xt}')headers['x-s'] = ret['X-s']
headers['x-t'] = str(ret['X-t'])url = 'https://edith.xiaohongshu.com'+api
response = requests.get(url, headers=headers, cookies=cookies,verify=False) # json格式的cookies
res = response.json()
print(res)
if not res["success"]:# {'code': -100, 'success': False, 'msg': '登錄已過期', 'data': {}}print("獲取失敗")sys.exit(1)
else:print('獲取成功')print(res["msg"])
?