【问题标题】:slack bot news feeder not banning words松弛的机器人新闻馈送不禁止文字
【发布时间】:2018-07-17 02:08:41
【问题描述】:

我无法让我的 slack bots 新闻馈送器禁止单词,触发词工作正常,但它让被禁止的词通过但它抓住了它们,因为它打印出 found banned word: xxxxx。我是 python 新手,只是不明白发生了什么。

triggers = ['SEC', 'CSA', 'OSC', 'CFTC', 'CME', 'CBOE', 'AMD', 'Intel', 'Nvidia',
            'Bitcoin', 'blockchain', 'Apple', 'Amazon', 'Google', 'Microsoft',
            'commerce', 'business', 'law', 'legal', 'financial', 'hack', 'hacked',
            'chains', 'chairman', 'CEO', 'board', 'bank']

banned = ['technical', 'analysis', 'bearish', 'bullish', 'trading', 'trade', 'opinion', 
          'sponsored', 'price', 'watch']

def feedparsecheck(url):
    feed = feedparser.parse(url)
    feed_title = feed['feed']['title']
    feed_entries = feed.entries
    database()
    print "feed 30 min"

    for entry in feed.entries:

        article_title = entry.title
        article_link = entry.link

        for trig in triggers:
            if trig.lower() in article_title.lower():#trigger
                for ban in banned:
                    if ban.lower() not in article_title.lower():#banned
                        response = "%s\n%s\n" % (article_title, article_link)
                        article_link = str(article_link.strip())

                        if not in_database(article_link):
                            update_database(article_link)
                            #print article_link
                            slack_client.api_call("chat.postMessage", channel=NEWS, text=''.join(response), as_user=True)
                    else:
                        print "found banned word:- " + ban

【问题讨论】:

    标签: python-2.7 rss bots feed slack


    【解决方案1】:

    您正在遍历所有文章的所有触发器和所有禁止字词,这意味着每篇文章都将针对标题中没有的每个禁止字词 * 标题中的触发器数发送到您的频道。

    例子:

    Bitcoin trading for lower price after hack 将被发送到您的频道 16 次。 2 个触发器(比特币,黑客)* 8 个不在标题中的禁用词(10 - len(交易,价格))= 16。

    修复:

    title_lower = article_title.lower()
    if any(trig.lower() in title_lower for trig in triggers):
        if any(ban.lower() in title_lower for ban in banned):
            print 'found banned word:- '+ ban
        else:
            # post to slack channel
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 2010-11-03
      • 2019-03-12
      相关资源
      最近更新 更多