如何检查响应类型aiohttp asyncio python

如何检查响应类型aiohttp asyncio python,python,python-asyncio,semaphore,aiohttp,Python,Python Asyncio,Semaphore,Aiohttp,我有以下方法: async def make_请求(self,url): 与aiohttp.ClientSession()作为会话异步: 以self.limit、session.get(url=url)作为响应的异步: resp=wait response.read() 等待异步睡眠(自速率) 返回响应 如何检查resp是否包含json?这样我就可以在json中格式化,例如json.dumps(resp)也许?如果resp类型是html,那么我必须遍历html树来提取resp值 我试过: as

我有以下方法:

async def make_请求(self,url):
与aiohttp.ClientSession()作为会话异步:
以self.limit、session.get(url=url)作为响应的异步:
resp=wait response.read()
等待异步睡眠(自速率)
返回响应
如何检查
resp
是否包含
json
?这样我就可以在
json
中格式化,例如
json.dumps(resp)
也许?如果resp类型是
html
,那么我必须遍历html树来提取
resp值

我试过:

async def make_请求(self,url):
与aiohttp.ClientSession()作为会话异步:
以self.limit、session.get(url=url)作为响应的异步:
resp=wait response.json()
等待异步睡眠(自速率)
返回响应

但是,如果
resp
包含如上所述的
html

,则会出错检查
内容类型
标题应该是第一步;但是,如果由于任何原因,标题缺失或不正确,您可以始终使用:

text = await response.text()
try:
    data = json.loads(text)
except ValueError as exc:
    print("cannot parse JSON: %s" % exc)
    # use text value 


可能检查内容类型标题。