Git Product home page Git Product logo

Comments (10)

ReaJason avatar ReaJason commented on July 30, 2024

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

from xhs.

jqsl2012 avatar jqsl2012 commented on July 30, 2024

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

image

from xhs.

jqsl2012 avatar jqsl2012 commented on July 30, 2024
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路径

from xhs.

ReaJason avatar ReaJason commented on July 30, 2024

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

from xhs.

jqsl2012 avatar jqsl2012 commented on July 30, 2024

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

from xhs.

ReaJason avatar ReaJason commented on July 30, 2024

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

是可以的哦

from xhs.

jqsl2012 avatar jqsl2012 commented on July 30, 2024

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

from xhs.

jqsl2012 avatar jqsl2012 commented on July 30, 2024

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

from xhs.

ReaJason avatar ReaJason commented on July 30, 2024

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

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

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

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

from xhs.

jqsl2012 avatar jqsl2012 commented on July 30, 2024

image

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

from xhs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.