代码拉取完成,页面将自动刷新
import json
import requests
from xhs_utils.xhs_util import get_headers, get_params, js, check_cookies, get_note_data, handle_note_info, norm_str, check_and_create_path, save_note_detail, download_media
class OneNote:
def __init__(self, cookies=None):
if cookies is None:
self.cookies = check_cookies()
else:
self.cookies = cookies
self.feed_url = 'https://edith.xiaohongshu.com/api/sns/web/v1/feed'
self.detail_url = 'https://www.xiaohongshu.com/explore/'
self.headers = get_headers()
self.params = get_params()
# 单个视频
def get_one_note_info(self, url):
note_id = url.split('/')[-1]
data = get_note_data(note_id)
data = json.dumps(data, separators=(',', ':'))
ret = js.call('get_xs', '/api/sns/web/v1/feed', data, self.cookies['a1'])
self.headers['x-s'], self.headers['x-t'] = ret['X-s'], str(ret['X-t'])
response = requests.post(self.feed_url, headers=self.headers, cookies=self.cookies, data=data)
res = response.json()
try:
data = res['data']['items'][0]
except:
print(f'笔记 {note_id} 不允许查看')
return
note = handle_note_info(data)
return note
# cover 是否覆盖
def save_one_note_info(self, url, need_cover=False, info='', dir_path='datas'):
try:
note = self.get_one_note_info(url)
nickname = norm_str(note.nickname)
user_id = note.user_id
title = norm_str(note.title)
if title.strip() == '':
title = f'无标题'
path = f'./{dir_path}/{nickname}_{user_id}/{title}_{note.note_id}'
new_img_path = f'./{dir_path}/{nickname}_{user_id}/all_pics'
check_and_create_path(new_img_path)
new_video_path = f'./{dir_path}/{nickname}_{user_id}/all_mp4'
check_and_create_path(new_video_path)
exist = check_and_create_path(path)
if exist and not need_cover:
print(f'用户: {nickname}, 标题: {title} 本地已存在,跳过保存')
return note
save_note_detail(path, note)
note_type = note.note_type
if note_type == 'normal':
for img_index, img in enumerate(note.image_list):
img_url = img['info_list'][1]['url']
#download_media(path, f'image_{img_index}', img_url, 'image', f'第{img_index}张图片')
download_media(new_img_path, f'{note.note_id}_image_{img_index}', img_url, 'image', f'第{img_index}张图片')
elif note_type == 'video':
img_url = note.image_list[0]['info_list'][1]['url']
download_media(new_img_path, f'{note.note_id}_cover', img_url, 'image', '视频封面')
# video_url = note.video_addr
# download_media(new_video_path, f'{note.note_id}_video', video_url, 'video')
print(f'用户: {nickname}, {info}标题: {title} 笔记保存成功')
print('===================================================================')
return note
except:
print(f'笔记 {url} 保存失败')
return
def main(self, url_list):
for url in url_list:
try:
self.save_one_note_info(url)
except:
print(f'笔记 {url} 保存失败')
continue
if __name__ == '__main__':
one_note = OneNote()
url_list = [
'https://www.xiaohongshu.com/explore/655376ba000000000f02ab7d',
"https://www.xiaohongshu.com/explore/653f2412000000002202f7b1",
"https://www.xiaohongshu.com/explore/640b3405000000000703924c",
"https://www.xiaohongshu.com/explore/65898a630000000010012ad1",
"https://www.xiaohongshu.com/explore/655b5d07000000000f02b361",
"https://www.xiaohongshu.com/explore/655f5e390000000032039862",
"https://www.xiaohongshu.com/explore/65689729000000003202c32c",
]
one_note.main(url_list)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。