python:TypeError:“协程”对象不可下标

时间:2018-07-21 20:19:21

标签: python python-asyncio

当我尝试从anticaptcha函数下标json对象时,我在此函数中不断遇到异常,但其他所有函数似乎都正常工作

'email':email, 'username': username, 'password': passwd, 'invite': None, 'captcha_key': await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']
TypeError: 'coroutine' object is not subscriptable

-

async def register(session, username, email, passwd):
    """
    sends a request to create an account
    """
    async with session.post('http://randomsite.com',
                            headers={
                                'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0'
                            },
                            json={
                                'email':email, 'username': username, 'password': passwd, 'invite': None, 'captcha_key': await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']
                            }) as response:
            return await response.json()

anticaptcha文件中的功能

async def task_result(session, task_id):
    """
    sends a request to return a specified captcha task result
    """
    while True:
        async with session.post('https://api.anti-captcha.com/getTaskResult',
                                json={
                                    "clientKey": ANTICAPTCHA_KEY,
                                    "taskId": task_id
                                }) as response:
            result = await response.json()
            if result['errorId'] > 0:
                print(colored('Anti-captcha error: ' + result['statusCode']), 'red')
            else:
                if result['status'] == 'ready':
                    return await result

async def solve(session, url):
   await get_balance(session)
   task_id = await create_task(session, url)['taskId']
   return await task_result(session, task_id)

1 个答案:

答案 0 :(得分:4)

await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']

表示

await (anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse'])

但是你想要

(await anticaptcha.solve(session, 'register'))['solution']['gRecaptchaResponse']

如果其他类似的事物task_id = await create_task(session, url)['taskId']在起作用,则它可能不会返回未来,您可以进行设置

task = create_task(session, url)['taskId']

没有await