报错'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', [])
时间: 2024-04-04 10:36:00 浏览: 5
这个报错通常是因为返回的数据为空,导致无法执行 `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'` 这个错误。