报错 'NoneType' object has no attribute 'get' 修改代码 def getData(id): url = "https://wzwlsgmh.wenzhou.gov.cn/api/Public/tableinfo" m = MultipartEncoder(fields={"platform_menu_id": id}) header = {'Content-Type': m.content_type} res = requests.post(url, data=m, headers=header) if res.content: res = res.json() else: return []
时间: 2024-04-04 07:35:45 浏览: 16
这个报错同样是因为返回的数据为空,导致无法执行 `get()` 方法。你可以在调用 `res.json()` 方法之前,先判断一下返回的 JSON 数据是否为空,比如:
```python
def getData(id):
url = "https://wzwlsgmh.wenzhou.gov.cn/api/Public/tableinfo"
m = MultipartEncoder(fields={"platform_menu_id": id})
header = {'Content-Type': m.content_type}
res = requests.post(url, data=m, headers=header)
if res.content:
res = res.json()
if res:
return res.get('data', [])
else:
return []
else:
return []
```
这样可以确保在返回的 JSON 数据为空时,不会执行 `res.get()` 方法,从而避免了出现 `'NoneType' object has no attribute 'get'` 这个错误。
相关问题
报错'NoneType' object has no attribute 'upper' 修改def getData(id): url = "https://wzwlsgmh.wenzhou.gov.cn/api/Public/tableinfo" m = MultipartEncoder(fields={"platform_menu_id": id}) header = {'Content-Type': m.content_type} res = requests.post(url, data=m, headers=header) res.raise_for_status() res = res.json() if res is None: return [] else: return res.get('data', [])
这个报错通常是因为返回的数据为空,导致无法执行 `upper()` 方法。为了避免这种情况,你可以在调用 `res.json()` 方法之前,先判断一下返回的 `response` 是否为空,比如:
```python
res = requests.post(url, data=m, headers=header)
if res.content:
res = res.json()
# rest of your code
else:
return []
```
这样可以确保在返回的数据为空时,不会执行 `res.json()` 方法,从而避免了出现 `'NoneType' object has no attribute 'upper'` 这个错误。
msg.get_body报错AttributeError: 'NoneType' object has no attribute 'get_content'
这个报错通常表示在获取email消息正文时出现了问题,因为NoneType类型的对象无法调用get_content()方法。这种情况通常有以下几种原因:
1. 消息的正文为空,即没有内容。
2. 消息的正文格式不被支持,导致无法获取其内容。
3. 由于网络等原因,邮件客户端没有正确地加载消息的内容。
要解决这个问题,可以尝试以下几种方法:
1. 检查消息是否确实存在正文内容。
2. 确认消息的正文格式是否被支持,如果不支持,可以尝试转换为支持的格式。
3. 检查网络连接,确保能够正确地加载消息内容。