我正在尝试解决一个练习,基本上,我必须解析一个 JSON 页面并搜索一个对象。如果未找到该对象,则我必须在下一页中搜索它。如果person我要查找的内容在第一页上,那么我通过了测试,但如果它在另一页上,则我会失败。我检查并正确解析了每个页面,但return如果它不在第一页上,则始终未定义。这是我的代码:import urllib.requestimport jsonclass Solution: def __new__(self, character): url = 'https://challenges.hackajob.co/swapi/api/people/' numberOfFilms = 0 # # Some work here; return type and arguments should be according to the problem's requirements # numberOfFilms = self.search(self,character,url) return numberOfFilms def search(self, character,url): numberOfFilms = 0 found = False with urllib.request.urlopen(url) as response: data = response.read() jsonData = json.loads(data.decode('utf-8')) for r in jsonData['results']: if r['name'] == character: return len(r['films']) if (jsonData['next']): nextPage = jsonData['next'] self.search(self,character,nextPage)
添加回答
举报
0/150
提交
取消