使用PRAW监控列表中的多个关键字字符串

时间:2018-07-22 06:53:33

标签: python reddit praw

我正在尝试使用PRAW设置一个自动搜索特定subreddits注释正文中的关键字的机器人。该漫游器随后将形成回复。现在,我只有一个要搜索的关键字。如何使它可以同时搜索多个关键字?

for comment in r.subreddit('watchpeopledie+morbidreality+wtf+fearme+horriblydepressing+truecreepy+creepy+scaredshitless+test').comments(limit=25):
    if "eyebleach" in comment.body and comment.id not in comments_replied_to and comment.author != r.user.me():
        print "String with \"eye bleach\" found in comment " + comment.id + "!"

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式定义包含多个关键字的搜索模式。

import re
pattern = r"\beye\s?bleach|\bcat\b|\dog\b"
match = r.search(pattern, comment.body)
if match:
    do stuff

此示例在“ comment.body”中搜索关键字“ eyebleach”,“ eye bleach”,“ cat”和“ dog”。 '\ b'是空格字符,因此我们实际上正在寻找被空格包围的“ eyebleach”。 如果找到这些单词中的任何一个,则match = True。