Python Praw 在 subreddits 中跳过粘滞
Python Praw skipping sticky in subreddits
我正在尝试遍历 subreddits,但想忽略顶部的置顶帖子。我能够打印前 5 个帖子,不幸的是包括即时贴。尝试跳过这些的各种 pythonic 方法都失败了。下面是我的代码的两个不同示例。
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)
我注意到文档中有一个粘性标签,但不确定如何使用它。感谢任何帮助。
It looks like you can get the id of a stickied post based on docs。因此,也许您可以获得粘性 post(s) 的 id(请注意,使用粘性方法的 'number' 参数,您可以说给我第一个、第二个或第三个, stickied post; 利用这个优势获得 stickied posts 的 all) 并且对于你要提取的每个提交,首先检查它的 id针对粘附的 ID。
示例:
# assuming there are no more than three stickies...
stickies = [reddit.subreddit("chicago").sticky(i).id for i in range(1,4)]
然后当你想确保给定的 post 没有被粘住时,使用:
if post.id not in stickies:
do something
看起来,如果少于三个,这会给您一个包含重复 ID 的列表,这不会有问题。
作为@Al Avery 回答的附录,您可以通过执行类似
的操作来完整搜索给定 subreddit 上所有即时贴的 ID
def get_all_stickies(sub):
stickies = set()
for i in itertools.count(1):
try:
sid = sub.sticky(i)
except pawcore.NotFound:
break
if sid in stickies:
break
stickies.add(sid)
return stickies
此函数考虑到如果向 stick
提供无效索引,文档会导致错误,而实际行为似乎是返回了重复的 ID。如果您有大量即时贴,使用 set
而不是列表可以加快查找速度。您将使用函数 as
subreddit = reddit.subreddit(sub)
stickies = get_all_stickies(subreddit)
for submission in subreddit.hot(limit=5):
if submission.id not in posts_replied_to and submission.id not in stickies:
print(submission.title)
被粘贴的提交有一个 sticked
属性,其计算结果为 True
。将以下内容添加到您的循环中,您应该可以开始了。
if submission.stickied:
continue
一般来说,我建议检查您正在使用的对象的可用属性,看看是否有可用的东西。参见:Determine Available Attributes of an Object
我正在尝试遍历 subreddits,但想忽略顶部的置顶帖子。我能够打印前 5 个帖子,不幸的是包括即时贴。尝试跳过这些的各种 pythonic 方法都失败了。下面是我的代码的两个不同示例。
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)
我注意到文档中有一个粘性标签,但不确定如何使用它。感谢任何帮助。
It looks like you can get the id of a stickied post based on docs。因此,也许您可以获得粘性 post(s) 的 id(请注意,使用粘性方法的 'number' 参数,您可以说给我第一个、第二个或第三个, stickied post; 利用这个优势获得 stickied posts 的 all) 并且对于你要提取的每个提交,首先检查它的 id针对粘附的 ID。
示例:
# assuming there are no more than three stickies...
stickies = [reddit.subreddit("chicago").sticky(i).id for i in range(1,4)]
然后当你想确保给定的 post 没有被粘住时,使用:
if post.id not in stickies:
do something
看起来,如果少于三个,这会给您一个包含重复 ID 的列表,这不会有问题。
作为@Al Avery 回答的附录,您可以通过执行类似
的操作来完整搜索给定 subreddit 上所有即时贴的 IDdef get_all_stickies(sub):
stickies = set()
for i in itertools.count(1):
try:
sid = sub.sticky(i)
except pawcore.NotFound:
break
if sid in stickies:
break
stickies.add(sid)
return stickies
此函数考虑到如果向 stick
提供无效索引,文档会导致错误,而实际行为似乎是返回了重复的 ID。如果您有大量即时贴,使用 set
而不是列表可以加快查找速度。您将使用函数 as
subreddit = reddit.subreddit(sub)
stickies = get_all_stickies(subreddit)
for submission in subreddit.hot(limit=5):
if submission.id not in posts_replied_to and submission.id not in stickies:
print(submission.title)
被粘贴的提交有一个 sticked
属性,其计算结果为 True
。将以下内容添加到您的循环中,您应该可以开始了。
if submission.stickied:
continue
一般来说,我建议检查您正在使用的对象的可用属性,看看是否有可用的东西。参见:Determine Available Attributes of an Object