函数进入循环并崩溃

2024-05-19 11:30:36 发布

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

def retrieve_jokes():
    before = random.randrange(0, 879)
    index = random.randrange(0, 99)

    subs = random.choice(["darkjokecentral", "darkjokes"])
    link = "https://api.pushshift.io/reddit/search/submission/?subreddit=" + subs + "&fields=" \
           "title,selftext,author&size=100&before=" + str(before) + "d&sort=asc&sort_type=score&score=>25"

    resp = requests.get(link)
    jsn = resp.json()
    k = jsn["data"]
    jokes = k[index]
    author = jokes["author"]
    title = jokes["title"]
    selftext = jokes["selftext"]
    total_len = len(selftext) + len(author) + len(title)
    if '[deleted]' in selftext or author or title:
        retrieve_jokes()
    elif total_len >= 280:
        retrieve_jokes()
    elif author is None or selftext is None or title is None:
        retrieve_jokes()
    return [author, title, selftext]

我正在使用pushshift api开发一个Twitter机器人;特维, 给定的代码是执行该工作的函数,但这有一个问题,即使我只在函数进入循环并最终崩溃时调用它。我不能把我的头围绕这个问题,任何形式的帮助是感激的


Tags: ornoneindexlentitleislinkrandom

热门问题