Skip to content

Commit

Permalink
MOD:小红书动态获取xsec_token
Browse files Browse the repository at this point in the history
  • Loading branch information
ShilongLee committed Jul 29, 2024
1 parent 370eb92 commit 0c555e1
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions service/xhs/logic/detail.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
from .common import common_request
from .common import common_request, COMMON_HEADERS
from lib import requests
from lib import logger
import re



async def request_detail(id: str, cookie: str) -> tuple[dict, bool]:
"""
请求小红书获取视频信息
"""
# 获取xsec_token
url = 'https://www.xiaohongshu.com/explore'
headers = {"cookie": cookie}
headers.update(COMMON_HEADERS)
resp = await requests.get(url, headers=headers)
if resp.status_code != 200 or resp.text == '':
return {}, False
pattern = r'xsec_token=(.*?)&'
match = re.search(pattern, resp.text)
if not match:
logger.error("No xsec_token match found.")
return {}, False
xsec_token = match.group(1)

# 获取详情
params = {"source_note_id": id, "image_formats": [
"jpg", "webp", "avif"], "extra": {"need_body_topic": "1"}}
"jpg", "webp", "avif"], "extra": {"need_body_topic": "1"}, "xsec_source": "pc_feed", "xsec_token": xsec_token}
headers = {"cookie": cookie}
resp, succ = await common_request('/api/sns/web/v1/feed', params, headers)
if not succ:
Expand Down

0 comments on commit 0c555e1

Please sign in to comment.