首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python: TypeError:‘协程’对象不可订阅

python: TypeError:‘协程’对象不可订阅
EN

Stack Overflow用户
提问于 2018-07-22 04:19:21
回答 1查看 17.9K关注 0票数 6

在这个函数中,当我试图将json对象的下标设置为anticaptcha函数的下标时,我总是得到一个异常,但所有其他函数似乎都工作得很好。

'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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-22 04:24:21

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的话。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51459793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档