Skip to content

Commit 4cdfeaf

Browse files
authored
fix: get note detail (#137)
1 parent 4b55f82 commit 4cdfeaf

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

example/basic_usage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def sign(uri, data=None, a1="", web_session=""):
4747
for _ in range(10):
4848
# 即便上面做了重试,还是有可能会遇到签名失败的情况,重试即可
4949
try:
50-
note = xhs_client.get_note_by_id("6505318c000000001f03c5a6")
50+
note = xhs_client.get_note_by_id("6505318c000000001f03c5a6", "xsec_token of the note")
5151
print(json.dumps(note, indent=4))
5252
print(help.get_imgs_url_from_note(note))
5353
break

xhs/core.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,25 @@ def post(self, uri: str, data: dict | None, is_creator: bool = False, is_custome
203203
else:
204204
return self.request(method="POST", url=f"{endpoint}{uri}", **kwargs)
205205

206-
def get_note_by_id(self, note_id: str):
206+
def get_note_by_id(self, note_id: str, xsec_token: str, xsec_source: str = "pc_feed"):
207207
"""
208208
:param note_id: note_id you want to fetch
209209
:type note_id: str
210210
:rtype: dict
211211
"""
212-
data = {"source_note_id": note_id, "image_scenes": ["CRD_WM_WEBP"]}
212+
213+
data = {
214+
"source_note_id": note_id,
215+
"image_formats": ["jpg", "webp", "avif"],
216+
"extra": {"need_body_topic": 1},
217+
"xsec_source": xsec_source,
218+
"xsec_token": xsec_token
219+
}
213220
uri = "/api/sns/web/v1/feed"
214221
res = self.post(uri, data)
215222
return res["items"][0]["note_card"]
216223

217-
def get_note_by_id_from_html(self, note_id: str):
224+
def get_note_by_id_from_html(self, note_id: str, xsec_token: str, xsec_source: str = "pc_feed"):
218225
"""get note info from "https://www.xiaohongshu.com/explore/" + note_id,
219226
and the return obj is equal to get_note_by_id
220227
@@ -245,7 +252,7 @@ def transform_json_keys(json_data):
245252
dict_new[new_key] = value
246253
return dict_new
247254

248-
url = "https://www.xiaohongshu.com/explore/" + note_id
255+
url = f"https://www.xiaohongshu.com/explore/{note_id}?xsec_token={xsec_token}&xsec_source={xsec_source}"
249256
res = self.session.get(url, headers={"user-agent": self.user_agent, "referer": "https://www.xiaohongshu.com/"})
250257
html = res.text
251258
state = re.findall(r"window.__INITIAL_STATE__=({.*})</script>", html)[0].replace("undefined", '""')
@@ -463,11 +470,10 @@ def get_user_all_notes(self, user_id: str, crawl_interval: int = 1):
463470
res = self.get_user_notes(user_id, cursor)
464471
has_more = res["has_more"]
465472
cursor = res["cursor"]
466-
note_ids = map(lambda item: item["note_id"], res["notes"])
467473

468-
for note_id in note_ids:
474+
for item in res["notes"]:
469475
try:
470-
note = self.get_note_by_id(note_id)
476+
note = self.get_note_by_id(item["note_id"], item["xsec_token"])
471477
except DataFetchError as e:
472478
if ErrorEnum.NOTE_ABNORMAL.value.msg in e.__repr__() or ErrorEnum.NOTE_SECRETE_FAULT.value.msg in e.__repr__():
473479
continue

0 commit comments

Comments
 (0)