我正在使用Facebook API并查看帖子的反应。 FB将响应限制为最多100条记录,但是分页以获得下一批等等。
我的测试案例有超过100个反应(最后计数:550)。我想要返回的是一个元组列表,其中每个元组都是对帖子的反应。
这是我到目前为止...输出列表比预期的少100多 -
import requests
def get_next_linksv3(page):
while True:
#try:
r = requests.get(page)
r_json = r.json()
if 'paging' in r_json.keys() and 'next' in r_json['paging']:
page = r_json['paging']['next']
yield page
else:
return
def process_follow_on_reactions_link(next_link, list_reaction_tuples):
nr = requests.get(next_link)
r_json = nr.json()
tmp_list = []
if 'data' in r_json.keys():
for nrecord in r_json['data']:
reaction_id = nrecord['id']
reaction_id_name = nrecord['name']
reaction_type = nrecord['type']
tmp_list.append((reaction_id,reaction_id_name,reaction_type))
return tmp_list
def process_initial_reactions_link(link):
list_reaction_tuples = []
r = requests.get(link)
r_json = r.json()
if 'reactions' in r_json.keys():
for record in r_json['reactions']['data']:
reaction_id = record['id']
reaction_id_name = record['name']
reaction_type = record['type']
list_reaction_tuples.append((reaction_id,reaction_id_name,reaction_type))
if 'reactions' in r_json.keys() and 'paging' in r_json['reactions'] and 'next' in r_json['reactions']['paging']:
next_link = r_json['reactions']['paging']['next']
gen = get_next_linksv3(next_link)
while True:
try:
list_reaction_tuples = list_reaction_tuples + (process_follow_on_reactions_link(next(gen), list_reaction_tuples))
except StopIteration:
return list_reaction_tuples
return list_reaction_tuples
tuple_list = process_initial_reactions_link(target_link)
答案 0 :(得分:0)
它可能会挂起等待某些数据或循环旋转。最有可能在while True
循环中。当它得到不正确的(或产生除所需的其他输出)URL(if
条件不满足)时,生成器不断下载相同的页面而永远不会产生。您可能需要引入一些max_retries
变量而不是while True
循环。
在调试器下运行程序并精确定位它挂起的位置。如果不可能 - 使用简单的打印调试方法。并向我们展示踪迹。