2

我试图将使用 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 中工作?

4

1 回答 1

4

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

values = await page.evaluate('''() => [...document.querySelectorAll('.table')]
                   .map(element => element.getAttribute('data-options'))
    ''')
于 2018-11-04T02:25:03.533 回答