gpt4 book ai didi

python - pyppeteer 和 javascript 查询

转载 作者:行者123 更新时间:2023-12-01 08:50:53 32 4
gpt4 key购买 nike

我正在尝试将使用 puppeteer 的节点项目迁移到使用 pyppeteer 的 python 项目。

我有下面的 javascript 查询,它工作正常。

const values = await page.evaluate(
() => [...document.querySelectorAll('.table')]
.map(element => element.getAttribute('data-options'))
);

当我尝试在 python/pyppeteer 中执行相同操作时:

values = await page.evaluate('''() = > [...document.querySelectorAll('.table')]
.map(element= > element.getAttribute('data-options'))
''')

我收到此错误:

Traceback (most recent call last):
File "/home/user/PycharmProjects/pytabledata/main.py", line 30, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "/usr/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
return future.result()
File "/home/user/PycharmProjects/pytabledata/main.py", line 23, in main
}''')
File "/home/user/Envs/pytabledata/lib/python3.6/site-packages/pyppeteer/page.py", line 1158, in evaluate
return await frame.evaluate(pageFunction, *args, force_expr=force_expr)
File "/home/user/Envs/pytabledata/lib/python3.6/site-packages/pyppeteer/frame_manager.py", line 295, in evaluate
pageFunction, *args, force_expr=force_expr)
File "/home/user/Envs/pytabledata/lib/python3.6/site-packages/pyppeteer/execution_context.py", line 55, in evaluate
pageFunction, *args, force_expr=force_expr)
File "/home/user/Envs/pytabledata/lib/python3.6/site-packages/pyppeteer/execution_context.py", line 114, in evaluateHandle
helper.getExceptionMessage(exceptionDetails)))
pyppeteer.errors.ElementHandleError: Evaluation failed: ReferenceError: Invalid left-hand side in assignment

如何使查询在 python 中工作?

最佳答案

JavaScript 代码在箭头运算符上存在拼写错误:= >(多余空格)。修复它们后,代码在我的机器上运行良好:

values = await page.evaluate('''() => [...document.querySelectorAll('.table')]
.map(element => element.getAttribute('data-options'))
''')

关于python - pyppeteer 和 javascript 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137043/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com