不出意外,我应该是打算写一下xhs_web抓取的一系列文章,初步确定下规模三篇左右
第一篇:note_list & note_detail 抓取
第二篇:xhs_cookie 获取
第三篇:解决xhs风控
难度由易到难,循序渐进
下面进去第一篇,这个真没什么好说的,直接放代码
# -*- coding:utf-8 -*-
import requests
def get_note_list(user_id):
response = requests.get(
url='https://www.xiaohongshu.com/user/profile/' + user_id,
headers={
'cookie': 'timestamp2=20211126da8572d5ea8a7a3a0e2874a3'
}
)
if 'captcha' in response.url:
return '触发滑动验证码'
else:
return response.content.decode('utf-8')
def get_note_detail(note_id):
response = requests.get(
url='https://www.xiaohongshu.com/discovery/item/' + note_id,
headers={
'cookie': 'timestamp2=20211126da8572d5ea8a7a3a0e2874a3'
}
)
if 'captcha' in response.url:
return '触发滑动验证码'
else:
return response.content.decode('utf-8')
if __name__ == '__main__':
print(get_note_list('559ba95cf5a263177913fb00'))
print(get_note_detail('619e4eb4000000000102f68e'))
算了,我还是说下过程吧
- 找到笔记列表链接和笔记详情链接,多找几篇,便可以找到共同点
- chrome打开F12,发送请求,在控制台找到该请求,右键 -> Copy -> Copy as cURL
- postman -> Import -> Raw text -> Continue -> Import -> Send
- 复制postman中代码至编辑器,修修改改,去除headers中不必要的字段,以及加入触发验证码提醒
需要注意的地方
- 代码仅实现了获取网页源码,数据解析需要你自己实现
- cookie中timestamp2的值来自哪,是下一篇内容
- 本文仅供学习参考, 请勿用作非法用途
- 如若涉及侵权, 416828817@qq.com/18355094977@163.com, 收到必删除!!!