Python PRAW Wrapper,逻辑问题

时间:2013-04-28 13:11:13

标签: python api if-statement while-loop reddit

我打算做的是从Reddit获得最高提交,将其附加到数组然后获得第二个提交,第三,第四等。我正在使用place_holder获取下一个提交,第一次工作,然后只是循环通过一次又一次地获得相同的,第二次提交。

当前输出为
发布1 邮政2
邮政2
等等

当我想要输出为
时 发布1 邮政2
邮政3
发布4

这是我的代码:

import praw, time

r = praw.Reddit(user_agent='ADNPost')

already_done = []

while True:
    for submission in r.get_top(limit=1):
        id = submission.id
        title = submission.title
        url = submission.short_link
        save_state = (id)
        if id not in already_done:
            already_done.append(submission.id)
            post = title + " | " + url
            print post
            print save_state
        if id in already_done:
            for submission in r.get_front_page(limit=1, place_holder=submission.id):
                id = submission.id
                title = submission.title
                url = submission.short_link
                print title, url
                save_state = (id)
                already_done.append(submission.id)
    time.sleep(2)   

1 个答案:

答案 0 :(得分:1)

你忘了时间。遵守reddit政策

我用一套效率

替换了yet_done

基本想法是在提交已经完成时获得新的提交

import praw, time

r = praw.Reddit(user_agent='ADNPost')

already_done = set()

while True:
    l = r.get_top(limit=1)
    submission = next(l,None)

    if not submission: 
        continue

    while submission.id in already_done:
        submission=next(r.get_front_page(limit=1, params={'after':submission.fullname}),None)
        if not submission:
            break
    if submission:
        id = submission.id
        title = submission.title
        url = submission.short_link
        print (title, url)
        save_state = (id)
        already_done.add(submission.id)
    time.sleep(2)

编辑:测试了place_holder不是你想要的(“t3_”是一个看起来有效的常量

编辑2 :根据@bboe命题将submission.fullname替换为“t3 _”+ submission.id