我想尝试使用pyppeteer包,但感觉使用Python 3.6.3 (default, Oct 3 2017, 21:45:48)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
[15:42:45]>>> import pyppeteer
[15:42:47]>>> browser = pyppeteer.launch()
[15:42:49]>>> pg = browser.newPage()
[15:42:52]>>> pg.goto('about:blank')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'coroutine' object has no attribute 'goto'
[15:42:55]>>> exit()
sys:1: RuntimeWarning: coroutine 'Browser.newPage' was never awaited
对我的小脚本来说太过分了。有没有办法同步运行异步代码?这是我试过的:
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
答案 0 :(得分:-1)
您需要使用aioconsole
这样的包作为相同的
pip3 install aioconsole
之后运行python
而不是运行apython
,然后您可以在Python REPL中使用await
语句
>>> import pyppeteer
>>> browser = pyppeteer.launch()
>>> pg = await browser.newPage()
>>> await pg.goto('http://tarunlalwani.com')
<pyppeteer.network_manager.Response object at 0x10ee84860>
>>> resp = _
>>> vars(resp)
{'_client': <pyppeteer.connection.Session object at 0x10ee5f940>, '_request': <pyppeteer.network_manager.Request object at 0x10ee840f0>, 'status': 200, '_contentPromise': <Future pending>, 'ok': True, 'url': 'http://tarunlalwani.com/', '_headers': {'date': 'Sat, 10 Mar 2018 02:28:04 GMT', 'content-encoding': 'gzip', 'last-modified': 'Sun, 10 Sep 2017 22:04:41 GMT', 'server': 'GitHub.com', 'x-github-request-id': 'E7AC:5FCC:6030CBE:86341C1:5AA342B4', 'vary': 'Accept-Encoding', 'content-type': 'text/html; charset=utf-8', 'access-control-allow-origin': '*', 'cache-control': 'max-age=600', 'transfer-encoding': 'chunked', 'expires': 'Sat, 10 Mar 2018 02:38:04 GMT'}}