获取小红书某个用户列表

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("====================================")

相关推荐
炸薯条!12 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
tryCbest13 小时前
Python 爬虫从入门到实战(二):进阶篇——反爬对抗、并发与动态页面
开发语言·爬虫·python
VIP_CQCRE13 小时前
GPT Image 2.6 动态表情交付别只发预览:GIF 文件校验、哈希清单与下载排错
图像处理·人工智能·python·gif
2601_9679873713 小时前
使用 NumPy 和 Python 实现梯度下降
python·机器学习·线性回归·numpy·梯度下降
BD_Marathon14 小时前
Jupyter魔法命令
ide·python·jupyter
词却14 小时前
Python 正则表达式实战:使用 re 模块提取符合条件的字符串
python·正则表达式
汤姆宝库15 小时前
视觉语言模型的实现:LLaVA式投影与指令微调
python·深度学习·语言模型
2601_967979121 天前
初学者必知的Python中优雅的用法
python·枚举·初学者·优雅用法·字典解析
AI创界者1 天前
Python 进阶实战(五十八章):配置热更新、优雅停机排空与高并发压测方案
人工智能·python·aigc
2601_967987371 天前
python网络工程师自动化运维课程
python·正则表达式·网络工程师·模块·自动化运维