获取小红书某个用户列表

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('//div[text()=" 用户"]').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('//div[text()=" - 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("====================================")

相关推荐
Robot侠6 小时前
极简LLM入门指南4
大数据·python·llm·prompt·提示工程
等....7 小时前
Miniconda使用
开发语言·python
Java&Develop7 小时前
Aes加密 GCM java
java·开发语言·python
爱笑的眼睛118 小时前
超越MSE与交叉熵:深度解析损失函数的动态本质与高阶设计
java·人工智能·python·ai
Rose sait9 小时前
【环境配置】Linux配置虚拟环境pytorch
linux·人工智能·python
过期动态10 小时前
JDBC高级篇:优化、封装与事务全流程指南
android·java·开发语言·数据库·python·mysql
一世琉璃白_Y10 小时前
pg配置国内数据源安装
linux·python·postgresql·centos
liwulin050610 小时前
【PYTHON】COCO数据集中的物品ID
开发语言·python
小鸡吃米…10 小时前
Python - XML 处理
xml·开发语言·python·开源
我赵帅的飞起10 小时前
python国密SM4加解密
python·sm4加解密·国密sm4加解密