由于UserAgent问题,AIOHTTP客户端超时

2024-09-30 14:32:30 发布

您现在位置:Python中文网/ 问答频道 /正文

aiohttp客户端遇到问题。
问题:我需要通过https url下载图像,它与requests.get()配合使用效果很好,但由于aiohttp超时而失败。在

以下是失败的例子:

url = "https://www.miamiherald.com/wps/source/images/miamiherald/facebook.jpg"
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

async with aiohttp.ClientSession().get(url, headers=headers) as response:
    content = await response.read()

得到:

^{pr2}$

同时,requests可以很好地处理相同的标题!在

url = "https://www.miamiherald.com/wps/source/images/miamiherald/facebook.jpg"
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

r = requests.get(url, headers = headers, stream=True)

有谁能帮我把它用在这个例子上吗?在


Tags: httpscomurlsourcegetfacebookaiohttpwww
1条回答
网友
1楼 · 发布于 2024-09-30 14:32:30

只需将超时参数传递给cs.get(timeout=...)cs(timeout=...)。这是文档https://docs.aiohttp.org/en/stable/client_quickstart.html#timeouts

示例:

import asyncio
import aiohttp
from aiohttp.client import ClientTimeout


async def test():
    url = "https://www.google.com/photos/about/static/images/google.svg"
    headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
    }

    timeout = ClientTimeout(total=0)  # `0` value to disable timeout
    cs = aiohttp.ClientSession()

    async with cs.request('get', url, headers=headers, timeout=timeout) as response:
        print(response.status)
        content = await response.read()

    await cs.close()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test())

相关问题 更多 >