1

私は Python Reddit API Wrapper (PRAW) を使用して、Reddit から特定のコメントを収集してきました。よく使用する機能の 1 つはreplace_more_comments()、スレッドのすべてのコメントを収集することです。

これらのスレッドの一部は非常に大きく (たとえば 10,000 コメント)、すべてのコメントを収集するのに時間がかかります。の進行状況バーを表示する方法はありreplace_more_comments()ますか?

最小限の作業コード サンプルを次に示します。

import praw
r = praw.Reddit('MSU vs Nebraska game')
submission = r.get_submission(submission_id='3rxx3y')
flat_comments = praw.helpers.flatten_tree(submission.comments)
submission.replace_more_comments(limit=None, threshold=0)
all_comments = submission.comments
flat_comments = praw.helpers.flatten_tree(submission.comments)
4

1 に答える 1

0

の組み込み実装はreplace_more_commentsこれをサポートしていませんが、独自のバージョンを作成できます。参考までに、元の実装を次に示します

実際の進行状況バーを描画する方法がわかりません。書く必要がありますupdate_progress_bar。このコードもテストしていないため、まったく機能しない可能性があります。

def replace_more_comments(self, post):
    """Update the comment tree by replacing instances of MoreComments."""
    if post._replaced_more:
        return

    more_comments = post._extract_more_comments(comment.comments)

    # Estimate the total number of comments
    count = 0
    for item in more_comments:
        count += item.count

    update_progress_bar(0, count)

    num_loaded = 0

    while more_comments:
        item = heappop(more_comments)

        # Fetch new comments and decrease remaining if a request was made
        new_comments = item.comments(update=False)
        elif new_comments is None:
            continue

        # Re-add new MoreComment objects to the heap of more_comments
        for more in self._extract_more_comments(new_comments):
            more._update_submission(post)  # pylint: disable=W0212
            heappush(more_comments, more)
        # Increase progress bar
        num_loaded += len(new_comments)
        update_progress_bar(num_loaded, count)
        # Insert the new comments into the tree
        for comment in new_comments:
            post._insert_comment(comment)

    post._replaced_more = True
于 2015-11-24T18:09:36.567 に答える