Get 方法没有 return 定义可选值
Get method doesn't return defined optional value
这是我正在使用的代码:
newResults = True
pages = 9
while newResults:
Request = f"{url}?key={API_Key}&ts={ts}&hash={hash}&project_id={project_id}&per_page= {per_page}&page={pages}&date_from={date_from}&date_to={date_to}"
r = requests.get(Request)
data = r.json()
for id in data['tickets']:
newResults = id.get('id', False)
pages += 1
一旦代码到达 newResults = id.get('id', False)
并且密钥不存在,它就会中断。尝试使用空列表,尝试使用字符串。尝试打印 newResult 以查看发生了什么 on.It 未打印可选值。
循环永远持续下去。
我哪里错了?
仍然不知道为什么 .get() 不会 return False,但这里有一个方法可以做到。
newResults = True
pages = 9
while newResults :
ticketsRequest = f"{url}?key={API_Key}&ts={ts}&hash={hash}&project_id={project_id}&per_page={per_page}&page={pages}&date_from={date_from}&date_to={date_to}"
r = requests.get(ticketsRequest)
data = r.json()
total = data['total']
for id in data['tickets']:
#stuff to do
result += 1
pages += 1
if result == total:
print (project_id, total, result)
break
这是我正在使用的代码:
newResults = True
pages = 9
while newResults:
Request = f"{url}?key={API_Key}&ts={ts}&hash={hash}&project_id={project_id}&per_page= {per_page}&page={pages}&date_from={date_from}&date_to={date_to}"
r = requests.get(Request)
data = r.json()
for id in data['tickets']:
newResults = id.get('id', False)
pages += 1
一旦代码到达 newResults = id.get('id', False)
并且密钥不存在,它就会中断。尝试使用空列表,尝试使用字符串。尝试打印 newResult 以查看发生了什么 on.It 未打印可选值。
循环永远持续下去。
我哪里错了?
仍然不知道为什么 .get() 不会 return False,但这里有一个方法可以做到。
newResults = True
pages = 9
while newResults :
ticketsRequest = f"{url}?key={API_Key}&ts={ts}&hash={hash}&project_id={project_id}&per_page={per_page}&page={pages}&date_from={date_from}&date_to={date_to}"
r = requests.get(ticketsRequest)
data = r.json()
total = data['total']
for id in data['tickets']:
#stuff to do
result += 1
pages += 1
if result == total:
print (project_id, total, result)
break