Python Praw 在 subreddits 中跳过粘性 [英] Python Praw skipping sticky in subreddits
问题描述
我正在尝试遍历 subreddits,但想忽略顶部的粘性帖子.我可以打印前 5 个帖子,不幸的是包括即时贴.试图跳过这些的各种 pythonic 方法都失败了.下面是我的代码的两个不同示例.
I am trying to loop through subreddits, but want to ignore the sticky posts at the top. I am able to print the first 5 posts, unfortunately including the stickies. Various pythonic methods of trying to skip these have failed. Two different examples of my code below.
subreddit = reddit.subreddit(sub)
for submission in subreddit.hot(limit=5):
# If we haven't replied to this post before
if submission.id not in posts_replied_to:
##FOOD
if subreddit == 'food':
if 'pLEASE SEE' in submission.title:
pass
if "please vote" in submission.title:
pass
else:
print(submission.title)
if re.search("please vote", submission.title, re.IGNORECASE):
pass
else:
print(submission.title)
我注意到文档中有一个粘性标签,但不确定如何使用它.任何帮助表示赞赏.
I noticed a sticky tag in the documents but not sure exactly how to use it. Any help is appreciated.
推荐答案
看来您可以根据文档获取已粘贴帖子的 ID.因此,也许您可以获得粘贴帖子的 id(请注意,使用粘贴方法的数字"参数,您可以说给我第一个、第二个或第三个粘贴的帖子;用它来您获得所有粘贴的帖子的优势)并且对于您要提取的每个提交,首先根据粘贴的 ID 检查其 ID.
It looks like you can get the id of a stickied post based on docs. So perhaps you could get the id(s) of the stickied post(s) (note that with the 'number' parameter of the sticky method you can say give me the first, or second, or third, stickied post; use this to your advantage to get all of the stickied posts) and for each submission that you are going to pull, first check its id against the stickied ids.
示例:
# assuming there are no more than three stickies...
stickies = [reddit.subreddit("chicago").sticky(i).id for i in range(1,4)]
然后当您想确保给定的帖子没有被置顶时,请使用:
and then when you want to make sure a given post isn't stickied, use:
if post.id not in stickies:
do something
看起来,如果少于三个,这会给你一个包含重复 ID 的列表,这不会有问题.
It looks like, were there fewer than three, this would give you a list with duplicate ids, which won't be a problem.
这篇关于Python Praw 在 subreddits 中跳过粘性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!