Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

登录流程实现 #46

Closed
furuiyang0715 opened this issue Sep 1, 2023 · 4 comments
Closed

登录流程实现 #46

furuiyang0715 opened this issue Sep 1, 2023 · 4 comments

Comments

@furuiyang0715
Copy link

你好,感谢项目提供的巨大帮助!

请问目前是否计划实现自动登录的流程, 用户端的操作,仅是扫二维码、收短信等, 不直接操作管理 cookie。

@ReaJason
Copy link
Owner

ReaJason commented Sep 1, 2023

不太理解你需要的自动登录流程是怎样的?
我理解的只能是半自动化的:

  1. 扫码,打印二维码,等待用户扫描成功,自动保存 cookie 后结束登录操作。
  2. 短信登录,发送短信,等待用户输入短信验证码,自动保存 cookie 后结束登录操作。

你的意思是 XhsClient 初始化默认不传入 cookie,然后根据登录操作来进行获取 cookie 和保存会话的操作吗?

@furuiyang0715
Copy link
Author

是这样的, 半自动化。

请问下面的两个流程现在有实现吗:
(1) 扫码,打印二维码,等待用户扫描成功,自动保存 cookie 后结束登录操作。
(2) 短信登录,发送短信,等待用户输入短信验证码,自动保存 cookie 后结束登录操作。
我找到了与二维码登录相关的两个封装接口:
get_qrcode 以及 check_qrcode
举例, 在 get_qrcode 接口返回:
{'code': 0, 'data': {'code': '764989', 'multi_flag': 0, 'qr_id': '532531693549495862', 'url': 'xhsdiscover://rn/app-settings/login/scan?qrId=532531693549495862&ruleId=4&code=764989&timestamp=1693549495876'}, 'msg': '成功', 'success': True}
用其参数输入 check_qrcode 返回:
{'code_status': 0}

这个流程我不是很清晰怎么获取到二维码图片以及在扫描后保存到 cookie

@furuiyang0715
Copy link
Author

即不希望输入是 cookie,这样对于非技术人员他们可能不是很理解这个参数第一次从哪里获取。
希望能有一个自动化或者半自动化获取 cookie 的封装。

@furuiyang0715
Copy link
Author

furuiyang0715 commented Sep 1, 2023

import os
import time
import loguru

from playwright.sync_api import Playwright, sync_playwright, expect
logger = loguru.logger
cur_project = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
logger.info(f"cur_project: {cur_project}")
stealth_js_path = os.path.join(cur_project, 'js', "stealth.min.js")


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context(
        viewport={"width": 1920, "height": 1080},
        user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 "
                   "Safari/537.36"
    )
    context.add_cookies([
        {'name': 'webId', 'value': "1", 'domain': ".xiaohongshu.com", 'path': "/"}
    ])
    context.add_init_script(path=stealth_js_path)

    page = context.new_page()
    page.goto("https://www.xiaohongshu.com/explore")
    time.sleep(10)
    page.get_by_placeholder("输入手机号").click()
    page.get_by_placeholder("输入手机号").fill("1562604xxxx")
    page.locator(".agree-icon > .reds-icon").click()
    page.get_by_text("获取验证码").click()

    code = input(f"请输入你收到的验证码:").strip()
    logger.warning(f"收到验证码{code}")
    page.get_by_placeholder("输入验证码").fill(code)
    time.sleep(10)

    page.get_by_role("button", name="登录").first.click()

    page.goto("https://www.xiaohongshu.com/explore")
    logger.warning(context.cookies())
    time.sleep(10)
    page.close()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)

目前我通过此脚本为第一次生成 cookie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants