RunTimError:Timeout context manager should be used in task——python3.8爬虫异步asyncio趟坑

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
乍一看,全是运行时错误,RunTimError:Timeout context manager should be used in task

# 异步抓取代码
async def fetch(session, url, headers=None, timeout=10, binary=False):
    _headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0",
    }
    if headers:
        _headers = headers
    try:
        async with session.get(url, headers=_headers, timeout=timeout)as response:
            status = response.status
            html = await response.read()
            if not binary:
                encoding = cchardet.detect(html)['encoding']
                html = html.decode(encoding, errors='ignore')
            redirected_url = str(response.url)
    except Exception as e:
        msg = "Failed to download url:{}, time:{},\n\t exception:{},\n{}".\format(url, time.strftime('%H:%M:%S'),
                                                                                 str(type(e)), str(e))
        print(msg)
        html = ""
        status = 0
        redirected_url = url
    return status, html, redirected_url

解决


请教了经验丰富的大佬,

1.

一开始,拿事件循环传给fetch异步请求函数,是放在__init__构造函数里面
在这里插入图片描述
python3.7以后版本,所有涉及异步的代码都要放到async函数里面
在这里插入图片描述
于是放到了这里,

2.


python3.7以后aiohttptimeout不能直接是整数,需要用aiohttptimeout类型

timeout = aiohttp.ClientTimeout(total=10)

再解决了其他几个bug后,就好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误信息“Uncaught (in promise) Error: timeout of 10000ms exceeded”通常是指在进行异步操作时超过了设定的超时时间。这可能是由于网络连接较慢或请求响应时间过长导致的。另外,根据引用的介绍,当引入第三方依赖的JS文件时可能也会出现这个错误。你可以检查一下你的代码是否正确引入了所需的文件。 解决这个问题的一个方法是在代码中增加超时时间设置。引用中提到,在Vue的主文件main.js中,你可以设置axios的超时时间。例如,你可以使用axios.defaults.timeout = 50000来将超时时间设置为50秒。这样做可以确保在请求超过50秒后,会抛出超时错误。你可以根据你的具体需求调整超时时间。 另外,还有一种方法是将axios封装到request中,以便更灵活地控制超时时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Uncaught (in promise) Error: timeout of 10000ms exceeded---SpringCloud Alibaba_若依微服务框架改造---...](https://blog.csdn.net/lidew521/article/details/115322871)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Vue项目报错:Uncaught SyntaxError: Unexpected token <](https://download.csdn.net/download/weixin_38593823/12949934)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Uncaught (in promise) Error: timeout of 5000ms exceeded](https://blog.csdn.net/qq_44774831/article/details/116376548)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值