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)
有谁能帮我把它用在这个例子上吗?在
只需将超时参数传递给
cs.get(timeout=...)
或cs(timeout=...)
。这是文档https://docs.aiohttp.org/en/stable/client_quickstart.html#timeouts示例:
相关问题 更多 >
编程相关推荐