python的requests返回response对象判断其内容的类型

可以使用response对象的属性content_type来判断返回内容的类型,例如:

```python
import requests

response = requests.get('https://www.example.com')
if 'text/html' in response.headers['content-type']:
    print('返回的内容是HTML文档')
elif 'application/json' in response.headers['content-type']:
    print('返回的内容是JSON格式')
else:
    print('未知的内容类型')
```

上述代码中,requests.get()方法返回的response对象具有headers属性,其中包含了返回内容的类型信息。我们使用in关键字判断其中是否包含了我们关注的特定类型,从而确定返回的内容类型。

相关代码参考