首页 > 解决方案 > aiohttp Clientsession 帖子卡住了

问题描述

我尝试使用 aiohttp 编写的 python 网络爬虫程序从 Quora 获取一些信息。我曾经aiohttp.ClientSession建立一个连接会话并设置超时aiohttp.ClientTimeout(total=5)。大多数时候,我的程序很好,但有时它会卡住,例如我试图从https://www.quora.com/graphql/gql_para_POST?q=EditCredentialsModalQueryaiohttp.ClientSession.post获取一些数据并检查响应状态。卡在这一步了。似乎无效。这是我的代码和错误图片:aiohttp.ClientTimeout(total=5)

        async with semaphore:
            while req_time <= 3:
                try:
                    async with session.post(url='https://www.quora.com/graphql/gql_para_POST?q'
                                                '=EditCredentialsModalQuery',
                                            headers=headers, data=payload, proxy=self.proxy) as resp:
                        if resp.status != 200:
                            print(f'{user_name}[ModalQuery]: resp.status is not 200, retrying {req_time}\n')
                            await asyncio.sleep(2)
                            req_time += 1
                            continue
                except Exception as e:
                    print(f'{user_name}[ModalQuery]: Async func get_cred_from_EditCredentialsModalQuery Error: {e}, '
                          f'retrying {req_time}')
                    await asyncio.sleep(2)
                    req_time += 1
                    continue

get_stuck_img

标签: posttimeoutaiohttp

解决方案


推荐阅读