代码说明:
1.最好先看前面 python 小红书处理记录(小程序)(一)获取关键词搜索出来的数据
2.获取用户信息这个会有风控,交给大家一个简单粗暴的方法:爬的时候,微信小程序自己也乱点点,看看这,看看那,爬的时候就会很顺畅
'''
Created on 2022年2月21日
@author: admin
'''
import hashlib
import requests
import warnings
import random
import pandas as pd
import time
warnings.filterwarnings("ignore")
bannerImage_list=[]
fans_list=[]
follows_list=[]
nickname_list=[]
notes_list=[]
def get_user(uid):
url=f"https://www.xiaohongshu.com/fe_api/burdock/weixin/v2/user/{uid}"
x_sign = 'X' + hashlib.md5(f"/fe_api/burdock/weixin/v2/user/{uid}WSUDD".encode(encoding='UTF-8')).hexdigest()
headers={
"Host": "www.xiaohongshu.com",
"Connection": "keep-alive",
"Authorization": "换成自己的",
"Device-Fingerprint": "换成自己的",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat"
,"X-Sign": f"{x_sign}",
"content-type": "application/json",
"Referer": "https://servicewechat.com/wxb296433268a1c654/60/page-frame.html",
"Accept-Encoding": "gzip, deflate, br"
}
#这里为了避免风控才整个随机获取headers,多整几个headers换着来爬更好
headers_list=[headers1]
res=requests.get(url=url,headers=random.choice(headers_list),verify=False)
print(res.json())
bannerImage_list.append(res.json()["data"]["bannerImage"])
fans_list.append(str(res.json()["data"]["fans"]))
follows_list.append(str(res.json()["data"]["follows"]))
nickname_list.append(res.json()["data"]["nickname"])
notes_list.append(res.json()["data"]["notes"])
dict_list={}
erro_list=[]
data=pd.read_excel("五谷磨房黑芝麻丸笔记数据.xlsx")["笔记用户id"].to_list()
for key,value in enumerate(data):
time.sleep(2)
print(key)
try:
get_user(value)
except:
erro_list.append(value)
pass
dict_list["头像"]=bannerImage_list
dict_list["粉丝数"]=fans_list
dict_list["关注人数"]=follows_list
dict_list["用户名"]=nickname_list
dict_list["发布笔记数"]=notes_list
data_=pd.Dataframe(dict_list)
data_.to_excel("五谷磨房黑芝麻丸用户信息.xlsx")
print(erro_list)



