14

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

'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

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)

2 Answers 2

29
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 = create_task(session, url)['taskId']

without await.

Sign up to request clarification or add additional context in comments.

1 Comment

basically the awaitable returns a (nested) dict and you can retrieve the values from the dict once the await is completed: (await function()) -> dict.
0

An alternative to the accepted method is one line longer, but adds a bit of clarity.

x = await anticaptcha.solve(session, 'register')    # first do the await
x = x['solution']['gRecaptchaResponse']             # then get the dict values

This above clearly shows what happens. When i first came across the same, i found the above helpful for clarity (in place of commenting the code).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.