我在这里用发电机是不是错了?

2024-05-20 11:00:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)

Tags: nameinidjsongettypepagelink
1条回答
网友
1楼 · 发布于 2024-05-20 11:00:15

它可能挂起等待一些数据或在循环中旋转。很可能在while True循环中。当它得到一个不正确的URL(或者产生一个不符合期望的输出)时(if条件不满足),生成器不断地下载同一个页面,并且从不产生任何结果。您可能需要引入一些max_retries变量,而不是while True循环。你知道吗

在调试器下运行程序并确定它挂起的位置。如果不可能-使用简单的打印调试方法。给我们看看痕迹。你知道吗

相关问题 更多 >