判读一个函数是不是协程

传入的是函数名,不需要加入括号:
def check_coroutine(fun):
if iscoroutinefunction(fun):
print('是协程')
else:
print('不是协程')

async def visit_web():
browser = await pyppeteer.launch(
{'headless': False,
'userDataDir': UserDataDir,
'defaultViewport': {'width': 1800, 'height': 1000},
'ignoreDefaultArgs':True,
}
)
page = await browser.newPage()

# 可以在launch下配置
# await page.setViewport({
# "width": 1900,
# "height": 1020
# })


# 先执行下面的JS 再去goto
await page.evaluate(
'''() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }''')
# await page.screenshot({'path': 'test.png', 'fullPage': True})
# await page.pdf({'path': 'test.pdf'})
# await asyncio.sleep(5)

await page.goto(url=URL)

# 这里的js是异步的写法
dimensions = await page.evaluate(
'''
()=>{
return {
width:document.documentElement.clientWidth,
height:document.documentElement.clientHeight,
deviceScaleFactor_:window.devicePixelRatio,
}
}
'''
)

result = await page.evaluate(
'''
()=>{
var title = document.title;
return {title:title};
}
'''

)


await browser.close()

调用:
check_coroutine(visit_web)
注意,上面不能用visit_web()

 

0 个评论

要回复文章请先登录注册