forked from cv-cat/Spider_XHS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (119 loc) · 5.72 KB
/
main.py
File metadata and controls
129 lines (119 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from loguru import logger
from apis.pc_apis import XHS_Apis
from xhs_utils.common_utils import init
from xhs_utils.data_util import handle_note_info, download_note, save_to_xlsx
class Data_Spider():
def __init__(self):
self.xhs_apis = XHS_Apis()
def spider_note(self, note_url: str, cookies_str: str, proxies=None):
"""
爬取一个笔记的信息
:param note_url:
:param cookies_str:
:return:
"""
note_info = None
try:
success, msg, note_info = self.xhs_apis.get_note_info(note_url, cookies_str, proxies)
if success:
note_info = note_info['data']['items'][0]
note_info['url'] = note_url
note_info = handle_note_info(note_info)
except Exception as e:
success = False
msg = e
logger.info(f'爬取笔记信息 {note_url}: {success}, msg: {msg}')
return success, msg, note_info
def spider_some_note(self, notes: list, cookies_str: str, base_path: dict, save_choice: str, excel_name: str = '', proxies=None):
"""
爬取一些笔记的信息
:param notes:
:param cookies_str:
:param base_path:
:return:
"""
if (save_choice == 'all' or save_choice == 'excel') and excel_name == '':
raise ValueError('excel_name 不能为空')
note_list = []
for note_url in notes:
success, msg, note_info = self.spider_note(note_url, cookies_str, proxies)
if note_info is not None and success:
note_list.append(note_info)
for note_info in note_list:
if save_choice == 'all' or save_choice == 'media':
download_note(note_info, base_path['media'])
if save_choice == 'all' or save_choice == 'excel':
file_path = os.path.abspath(os.path.join(base_path['excel'], f'{excel_name}.xlsx'))
save_to_xlsx(note_list, file_path)
def spider_user_all_note(self, user_url: str, cookies_str: str, base_path: dict, save_choice: str, excel_name: str = '', proxies=None):
"""
爬取一个用户的所有笔记
:param user_url:
:param cookies_str:
:param base_path:
:return:
"""
note_list = []
try:
success, msg, all_note_info = self.xhs_apis.get_user_all_notes(user_url, cookies_str, proxies)
if success:
logger.info(f'用户 {user_url} 作品数量: {len(all_note_info)}')
for simple_note_info in all_note_info:
note_url = f"https://www.xiaohongshu.com/explore/{simple_note_info['note_id']}?xsec_token={simple_note_info['xsec_token']}"
note_list.append(note_url)
if save_choice == 'all' or save_choice == 'excel':
excel_name = user_url.split('/')[-1].split('?')[0]
self.spider_some_note(note_list, cookies_str, base_path, save_choice, excel_name, proxies)
except Exception as e:
success = False
msg = e
logger.info(f'爬取用户所有视频 {user_url}: {success}, msg: {msg}')
return note_list, success, msg
def spider_some_search_note(self, query: str, require_num: int, cookies_str: str, base_path: dict, save_choice: str, sort="general", note_type=0, excel_name: str = '', proxies=None):
"""
指定数量搜索笔记,设置排序方式和笔记类型和笔记数量
:param query 搜索的关键词
:param require_num 搜索的数量
:param cookies_str 你的cookies
:param base_path 保存路径
:param sort 排序方式 general:综合排序, time_descending:时间排序, popularity_descending:热度排序
:param note_type 笔记类型 0:全部, 1:视频, 2:图文
返回搜索的结果
"""
note_list = []
try:
success, msg, notes = self.xhs_apis.search_some_note(query, require_num, cookies_str, sort, note_type, proxies)
if success:
notes = list(filter(lambda x: x['model_type'] == "note", notes))
logger.info(f'搜索关键词 {query} 笔记数量: {len(notes)}')
for note in notes:
note_url = f"https://www.xiaohongshu.com/explore/{note['id']}?xsec_token={note['xsec_token']}"
note_list.append(note_url)
if save_choice == 'all' or save_choice == 'excel':
excel_name = query
self.spider_some_note(note_list, cookies_str, base_path, save_choice, excel_name, proxies)
except Exception as e:
success = False
msg = e
logger.info(f'搜索关键词 {query} 笔记: {success}, msg: {msg}')
return note_list, success, msg
if __name__ == '__main__':
cookies_str, base_path = init()
data_spider = Data_Spider()
# save_choice: all: 保存所有的信息, media: 保存视频和图片, excel: 保存到excel
# save_choice 为 excel 或者 all 时,excel_name 不能为空
# 1
notes = [
r'https://www.xiaohongshu.com/explore/67d7c713000000000900e391?xsec_token=AB1ACxbo5cevHxV_bWibTmK8R1DDz0NnAW1PbFZLABXtE=&xsec_source=pc_user',
]
data_spider.spider_some_note(notes, cookies_str, base_path, 'all', 'test')
# 2
user_url = 'https://www.xiaohongshu.com/user/profile/67a332a2000000000d008358?xsec_token=ABTf9yz4cLHhTycIlksF0jOi1yIZgfcaQ6IXNNGdKJ8xg=&xsec_source=pc_feed'
data_spider.spider_user_all_note(user_url, cookies_str, base_path, 'all')
# 3
query = "榴莲"
query_num = 10
sort = "general"
note_type = 0
data_spider.spider_some_search_note(query, query_num, cookies_str, base_path, 'all', sort, note_type)