1

What I'm aiming to do is to get the top submission from Reddit, append it to an array and then get the second top submission, third, fourth, etc. I'm using place_holder to get the next submission, which works the first time but then just loops through getting the same, second, submission over and over again.

The current output is
Post 1
Post 2
Post 2
etc

When I want the output to be
Post 1
Post 2
Post 3
Post 4
etc

Here's my code:

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)   
4

1 回答 1

1

you forgot a time.sleep to comply with reddit policy

I replaced already_done with a set for efficiency

the basic idea is to getting new submission while the submission was already 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)

EDIT: tested the place_holder is not what you want (the "t3_" is a constant which looks working

EDIT 2: replaced "t3_"+submission.id by submission.fullname according to @bboe proposition

于 2013-04-28T15:29:34.083 回答