Documentation

The XhsClient class

class xhs.XhsClient(cookie=None, user_agent=None, timeout=10, proxies=None, sign=None)[源代码]

基类:object

activate()[源代码]
check_code(phone: str, code: str, zone: str = 86)[源代码]
check_qrcode(qr_id: str, code: str)[源代码]
collect_note(note_id: str)[源代码]
comment_note(note_id: str, content: str)[源代码]

comment a note

返回类型

dict

comment_user(note_id: str, comment_id: str, content: str)[源代码]

comment a user comment

参数
  • content – comment content

  • note_id – the id of the note

  • comment_id – target comment_id you want to comment

返回类型

dict

property cookie
property cookie_dict
create_complete_multipart_upload(file_id: str, token: str, upload_id: str, parts: list)[源代码]
create_image_note(title, desc, files: list, post_time: Optional[str] = None, ats: Optional[list] = None, topics: Optional[list] = None, is_private: bool = False)[源代码]

发布图文笔记

参数
  • title – 笔记标题

  • desc – 笔记详情

  • files – 文件路径列表,目前只支持本地路径

  • post_time – 可选,发布时间,例如 “2023-10-11 12:11:11”

  • ats – 可选,@用户信息

  • topics – 可选,话题信息

  • is_private – 可选,是否私密发布

返回

create_note(title, desc, note_type, ats: Optional[list] = None, topics: Optional[list] = None, image_info: Optional[dict] = None, video_info: Optional[dict] = None, post_time: Optional[str] = None, is_private: bool = False)[源代码]
create_video_note(title, video_path: str, desc: str, cover_path: Optional[str] = None, ats: Optional[list] = None, post_time: Optional[str] = None, topics: Optional[list] = None, is_private: bool = False, wait_time: int = 3)[源代码]

发布视频笔记

参数
  • title – 笔记标题

  • video_path – 视频文件路径,目前只支持本地路径

  • desc – 笔记详情

  • cover_path – 可选,封面文件路径

  • ats – 可选,@用户信息

  • post_time – 可选,发布时间

  • topics – 可选,话题信息

  • is_private – 可选,是否私密发布

  • wait_time – 可选,默认 3 s,循环等待获取视频第一帧为笔记封面

返回

返回类型

object

delete_note_comment(note_id: str, comment_id: str)[源代码]
dislike_comment(note_id: str, comment_id: str)[源代码]
dislike_note(note_id: str)[源代码]
follow_user(user_id: str)[源代码]
get(uri: str, params=None, is_creator: bool = False, **kwargs)[源代码]
get_emojis()[源代码]
get_follow_notifications(num: int = 20, cursor: str = '')[源代码]
get_home_feed(feed_type: xhs.core.FeedType)[源代码]
get_home_feed_category()[源代码]
get_like_notifications(num: int = 20, cursor: str = '')[源代码]
get_mention_notifications(num: int = 20, cursor: str = '')[源代码]
get_note_all_comments(note_id: str, crawl_interval: int = 1)[源代码]

get note all comments include sub comments

参数
  • crawl_interval – crawl interval for fetch

  • note_id (str) – note id you want to fetch

get_note_by_id(note_id: str)[源代码]
参数

note_id (str) – note_id you want to fetch

返回类型

dict

get_note_by_id_from_html(note_id: str)[源代码]

get note info from “https://www.xiaohongshu.com/explore/” + note_id, and the return obj is equal to get_note_by_id

参数

note_id (str) – note_id you want to fetch

get_note_by_keyword(keyword: str, page: int = 1, page_size: int = 20, sort: xhs.core.SearchSortType = SearchSortType.GENERAL, note_type: xhs.core.SearchNoteType = SearchNoteType.ALL)[源代码]

search note by keyword

参数
  • keyword (str) – what notes you want to search

  • page (int, optional) – page number, defaults to 1

  • page_size (int, optional) – page size, defaults to 20

  • sort (SearchSortType, optional) – sort ordering, defaults to SearchSortType.GENERAL.

  • note_type (SearchNoteType, optional) – note type, defaults to SearchNoteType.ALL.

返回

{has_more: true, items: []}

返回类型

dict

get_note_comments(note_id: str, cursor: str = '')[源代码]

get note comments

参数
  • note_id (str) – note id you want to fetch

  • cursor (str, optional) – last you get cursor, defaults to “”

返回类型

dict

get_note_sub_comments(note_id: str, root_comment_id: str, num: int = 30, cursor: str = '')[源代码]

get note sub comments

参数
  • note_id (str) – note id you want to fetch

  • root_comment_id (str) – parent comment id

  • num (int) – recommend 30, if num greater 30, it only returns 30 comments

  • cursor (str optional) – last you get cursor, defaults to “”

返回类型

dict

get_notes_statistics(page: int = 1, page_size: int = 48, sort_by='time', note_type=0, time=30, is_recent=True)[源代码]
参数
  • page – page num default is 1

  • page_size – page size, 12 or 24 or 36 or 48

  • sort_by – time default

  • note_type – 0 is all, 1 is images, 2 is video

  • time – fetch date

  • is_recent – default is false, when time is 7, this should be false

返回

get_notes_summary()[源代码]
get_qrcode()[源代码]

create qrcode, you can trasform response url to qrcode

返回

{“qr_id”:”87323168**”,”code”:”280148”,”url”:”xhsdiscover://**”,”multi_flag”:0}

返回类型

dict

get_search_suggestion(keyword: str)[源代码]
get_self_info()[源代码]
get_self_info2()[源代码]
get_suggest_ats(keyword='')[源代码]

通过关键词获取用户信息,发布笔记用

参数

keyword – 用户名关键词,如 ReaJason

返回

get_suggest_topic(keyword='')[源代码]

通过关键词获取话题信息,发布笔记用

参数

keyword – 话题关键词,如 Python

返回

get_upload_files_permit(file_type: str, count: int = 1) tuple[源代码]

获取文件上传的 id

参数
  • file_type – 文件类型,[“images”, “video”]

  • count – 文件数量

返回

get_upload_id(file_id, token)[源代码]
get_user_all_notes(user_id: str, crawl_interval: int = 1)[源代码]

get user all notes with more info, abnormal notes will be ignored

参数
  • user_id (str) – user_id you want to fetch

  • crawl_interval (int, optional) – sleep seconds, defaults to 1

返回

note info

返回类型

list[Note]

get_user_by_keyword(keyword: str, page: int = 1, page_size: int = 20)[源代码]
get_user_collect_notes(user_id: str, num: int = 30, cursor: str = '')[源代码]
get_user_info(user_id: str)[源代码]
参数

user_id (str) – user_id you want fetch

返回类型

dict

get_user_like_notes(user_id: str, num: int = 30, cursor: str = '')[源代码]
get_user_notes(user_id: str, cursor: str = '')[源代码]

get user notes just have simple info

参数
  • user_id (str) – user_id you want to fetch

  • cursor (str, optional) – return info has this argument, defaults to “”

返回

{cursor:””, has_more:true,notes:[{cover:{},display_title:””,interact_info:{},note_id:””,type:”video”}]}

返回类型

dict

get_video_first_frame_image_id(video_id: str)[源代码]
like_comment(note_id: str, comment_id: str)[源代码]
like_note(note_id: str)[源代码]
login_code(phone: str, mobile_token: str, zone: str = 86)[源代码]
post(uri: str, data: dict, is_creator: bool = False, **kwargs)[源代码]
report_note_metrics(note_id: str, note_type: int, note_user_id: str, viewer_user_id: str, followed_author=0, report_type=1, stay_seconds=0)[源代码]

report note stay seconds and other interaction info

参数
  • note_id (str) – note_id which you want to report

  • note_type (int) – input value -> 1: note is images, 2: note is video

  • note_user_id (str) – note author id

  • viewer_user_id (str) – report user id

  • followed_author (int) – 1: the viewer user follow note’s author, 0: the viewer user don’t follow note’s author

  • report_type (int, optional) – 1: the first report, 2: the second report, so you must report twice, defaults to 1

  • stay_seconds (int, optional) – report metric -> note you stay seconds, defaults to 0

返回

same as api

返回类型

dict

request(method, url, **kwargs)[源代码]
save_files_from_note_id(note_id: str, dir_path: str)[源代码]

this function will fetch note and save file in dir_path/note_title

参数
  • note_id (str) – note_id that you want to fetch

  • dir_path (str) – in fact, files will be stored in your dir_path/note_title directory

send_code(phone: str, zone: str = 86)[源代码]
property session
uncollect_note(note_id: str)[源代码]
unfollow_user(user_id: str)[源代码]
upload_file(file_id: str, token: str, file_path: str, content_type: str = 'image/jpeg')[源代码]

将文件上传至指定文件 id 处

参数
  • file_id – 上传文件 id

  • token – 上传授权验证 token

  • file_path – 文件路径,暂只支持本地文件路径

  • content_type – 【”video/mp4”,”image/jpeg”,”image/png”】

返回

upload_file_with_slice(file_id: str, token: str, file_path: str)[源代码]

The FeedType class

class xhs.FeedType(value)[源代码]

基类:enum.Enum

An enumeration.

CAREER = 'homefeed.career_v3'
COSMETICS = 'homefeed.cosmetics_v3'
EMOTION = 'homefeed.love_v3'
FASION = 'homefeed.fashion_v3'
FITNESS = 'homefeed.fitness_v3'
FOOD = 'homefeed.food_v3'
GAME = 'homefeed.gaming_v3'
HOURSE = 'homefeed.household_product_v3'
MOVIE = 'homefeed.movie_and_tv_v3'
RECOMMEND = 'homefeed_recommend'
TRAVEL = 'homefeed.travel_v3'