获取小红书某个用户列表

from playwright.sync_api import sync_playwright

import time

cookie=\[\] #用自己的cookie

def capture_response(response, url_to_capture, api_response_data):

检查响应的 URL 是否匹配

if url_to_capture in response.url:

检查是否是 JSON 格式

if 'application/json' in response.headers.get('content-type', ''):

api_response_data.append(response.json()) # 将 JSON 数据添加到列表中

p = sync_playwright().start()

browser = p.chromium.launch(headless=True)

context = browser.new_context()

context.add_cookies(cookie)

page = context.new_page()

api_response_data = \[\]

url_to_capture = 'https://edith.xiaohongshu.com/api/sns/web/v1/search/usersearch'

page.on('response', lambda response: capture_response(response, url_to_capture, api_response_data))

page.goto("https://www.xiaohongshu.com/search_result/?keyword=%25E5%25B0%258F%25E4%25B9%258C%25E8%258B%258F\&source=web_explore_feed\&type=51")

page.wait_for_load_state('load')

page.locator('//divtext()=" 用户"').click()

page.wait_for_event('response')

#page.wait_for_timeout(3000)

while True:

page.mouse.wheel(0, 1000)

#page.wait_for_timeout(1000)

try:

page.wait_for_selector('//divtext()=" - THE END - "',timeout=50)

print("下拉结束")

break

except Exception as e:

print("继续下拉")

pass

page.wait_for_load_state('load')

#print(page.content())

print(len(api_response_data))

#browser.close()

for k in api_response_data:

for i in k'data''users':

print(i'name')

print(i'sub_title')

print(i'image')

print("====================================")

相关推荐
如竟没有火炬23 分钟前
最大矩阵——单调栈
数据结构·python·线性代数·算法·leetcode·矩阵
阳区欠33 分钟前
【LangChain】LLM基础介绍
开发语言·python·langchain
Cosolar34 分钟前
保姆级 CrewAI 教程:从零构建多智能体协作系统
人工智能·python·架构
GDAL1 小时前
使用 uv 管理 Python 版本
python·uv·版本
真实的菜1 小时前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
cup111 小时前
[开源] Meta Assistant / 告别命令行,我为一堆 Python 脚本做了一个 Windows 任务栏的“家”
windows·python·工具·nuitka·脚本运行
小小编程路2 小时前
Python 还有容器类型互转、进制转换、字符编码转换
开发语言·windows·python
Samooyou2 小时前
RAG项目案例--02在线检索&过滤流水线
人工智能·python·ai·全文检索·检索
动能小子ohhh3 小时前
DocForge平台的设计与开发--文件上传接口的实现
开发语言·人工智能·python·langchain·ocr·fastapi
ab_dg_dp3 小时前
Android 17+ 提取 AIDL 生成 Java 文件的实用脚本
android·java·python