简体   繁体   English

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

[英]python: TypeError: 'coroutine' object is not subscriptable

i keep getting an exception in this function when it trys to subscript the json object from the anticaptcha function, but all the other functions seem to be working fine 当我尝试从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()

the functions from the anticaptcha file 来自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)
await anticaptcha.solve(session, 'register')['solution']['gRecaptchaResponse']

means 手段

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

but you want 但是你想要

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

If the other similar thing, task_id = await create_task(session, url)['taskId'] , is working, it probably doesn't return a future and you can just set 如果其他类似的task_id = await create_task(session, url)['taskId']正在运行,则可能不会返回将来,您可以设置

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

without await . 没有await

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM