1

在 python 中,使用pyppeteer打开网页在其控制台中运行 JS 脚本,并尝试在变量 e 中捕获结果,但出现以下错误。

Traceback (most recent call last):
  File "/home/ndaruto/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/naruto/anaconda3/lib/python3.7/site-packages/django/utils/deprecation.py", line 96, in __call__
    response = self.process_response(request, response)
  File "/home/naruto/anaconda3/lib/python3.7/site-packages/django/middleware/clickjacking.py", line 26, in process_response
    if response.get('X-Frame-Options') is not None:
AttributeError: 'coroutine' object has no attribute 'get'
/home/naruto/anaconda3/lib/python3.7/pathlib.py:704: RuntimeWarning: coroutine 'hmm' was never awaited


return self._str
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

以下是python代码:-

async def hmm(request):
browser = await launch()
page = await browser.newPage()
await page.goto('http://jobs.chegg.com')
ans = await page.evaluate('''() => {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/axe-core/3.1.2/axe.min.js';
    document.head.appendChild(script);
    setTimeout(function(){
    axe.run(document, {
        runOnly: {
        type: "tag",
        values: ["wcag2a", "wcag2aa", "best-practice"]
        },
        "rules": {
        "skip-link": { enabled: false }
        }
    }, function(err, results) {
        if (err) throw err;
        console.log(results);
    });
    }, 1000);
}''')
print("ANS IS", ans)
return 1

有人可以建议如何解决这个问题吗?

4

2 回答 2

1

AttributeError: 'coroutine' object has no attribute 'get' /home/naruto/anaconda3/lib/python3.7/pathlib.py:704: RuntimeWarning: coroutine 'hmm' was never awaited 这意味着您 在围绕此函数的代码hmm(request).get()

r = await hmm(request) r.get()
的某处调用

于 2020-02-20T13:57:36.517 回答
0

将需要更多上下文。

您的错误未引用脚本中的任何代码,但引用了未见过的对象。

听起来您要么错过了某个地方的“等待”,要么您需要通过 aysnc 工厂调用某些东西。

一旦我知道更多,我可以提供帮助。

我还建议升级到 3.8,因为 Asyncio 对 python 来说有点新,它不会受到伤害。

于 2020-02-10T19:57:51.900 回答