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

playwright._impl._api_types.Error: It looks like you are using Playwright Sync API inside the asyncio loop. Please use the Async API instead. #54

Closed
jqsl2012 opened this issue Sep 21, 2023 · 10 comments

Comments

@jqsl2012
Copy link

RT,大佬这是什么错误,我该如何消除

@ReaJason
Copy link
Owner

麻烦贴一下你 sign 函数,确保是最新的 basic_usage.py

@jqsl2012
Copy link
Author

@ReaJason 我刚才用了你这个最新的跑,分别在colab和阿里云服务器上运行,都报这个错误

image

@jqsl2012
Copy link
Author

import datetime
import json
from time import sleep
from xhs import XhsClient, DataFetchError, help
from playwright.sync_api import sync_playwright


def sign(uri, data=None, a1="", web_session=""):
    for _ in range(10):
        try:
            with sync_playwright() as playwright:
                stealth_js_path = "/root/notebook/spider/stealth.min.js"
                chromium = playwright.chromium

                # 如果一直失败可尝试设置成 False 让其打开浏览器,适当添加 sleep 可查看浏览器状态
                browser = chromium.launch(headless=True)

                browser_context = browser.new_context()
                browser_context.add_init_script(path=stealth_js_path)
                context_page = browser_context.new_page()
                context_page.goto("https://www.xiaohongshu.com")
                browser_context.add_cookies([
                    {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}]
                )
                context_page.reload()
                # 这个地方设置完浏览器 cookie 之后,如果这儿不 sleep 一下签名获取就失败了,如果经常失败请设置长一点试试
                sleep(1)
                encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data])
                return {
                    "x-s": encrypt_params["X-s"],
                    "x-t": str(encrypt_params["X-t"])
                }
        except Exception:
            # 这儿有时会出现 window._webmsxyw is not a function 或未知跳转错误,因此加一个失败重试趴
            pass
    raise Exception("重试了这么多次还是无法签名成功,寄寄寄")


if __name__ == '__main__':
    cookie = "abRequestId=5e46ecd7-fa81-5e48-9536-971d73d70d1b; xsecappid=xhs-pc-web; a1=18a69364557tmlno4bf3r1f2m70aumwwcyruyfsq450000130761; webId=10dc3026615715fcafc3efbc9c5a4725; gid=yY0KjqK444S2yY0KjqK42viM2W961Il4DiqFFA4yiJ6W8728076EEv888yq8WKy84yY8KqyS; web_session=040069b396e0e12e283c76173f374b19caf78c; webBuild=3.9.6; websectiga=634d3ad75ffb42a2ade2c5e1705a73c845837578aeb31ba0e442d75c648da36a"

    xhs_client = XhsClient(cookie, sign=sign)
    print(datetime.datetime.now())

    for _ in range(10):
        # 即便上面做了重试,还是有可能会遇到签名失败的情况,重试即可
        try:
            note = xhs_client.get_note_by_id("6505318c000000001f03c5a6")
            print(json.dumps(note, indent=4))
            print(help.get_imgs_url_from_note(note))
            break
        except DataFetchError as e:
            print(e)
            print("失败重试一下下")

我运行的完整代码,只改了cookie和stealth.min.js路径

@ReaJason
Copy link
Owner

本地能正常运行,报这个错误你可能需要自己改写成 async 的方式

@jqsl2012
Copy link
Author

你用上面代码可以本地运行吗

@ReaJason
Copy link
Owner

你用上面代码可以本地运行吗

是可以的哦

@jqsl2012
Copy link
Author

@ReaJason 那我这是什么问题。。。xhs或playwright的版本不对吗?

@jqsl2012
Copy link
Author

@ReaJason example的代码换了5个不同环境,只修改必须配置项,均不能运行。哥们,这项目没法推广呀

@ReaJason
Copy link
Owner

我不需要你推广项目,请不要自以为是,开源项目只是为了帮助大家(实在帮不到也是没办法的事情),项目的初衷在 README 已有写,只为练习一下 Python,顺便为了帮助到有需要的人。

你这样问问题,我没法帮助到你(可以麻烦贴一下,运行脚本的报错记录,你提醒到我 issue template 的重要性了)。

刚特地弄了个 Windows 11 虚拟机,也能正常跑,就是和文档一样的步骤。

CleanShot 2023-09-22 at 12 30 47@2x

@jqsl2012
Copy link
Author

image

我贴了呀,帖子标题,就是这个错误。
兄弟代码不稳定呀,其中一个环境啥也没动,昨天不行,今天跑一下居然出来了。

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