【问题标题】:Pattern matching Twitter Streaming API模式匹配 Twitter Streaming API
【发布时间】:2012-11-16 20:41:24
【问题描述】:

我正在尝试将来自 Streaming API 的某些值插入到字典中。其中之一是在使用 track=keyword 的过滤方法中使用的术语的值。我已经编写了一些代码,但在打印语句中我收到“遇到异常:'term'”错误。这是我的部分代码:

 for term in setTerms:
     a = re.compile(term, re.IGNORECASE)
     if re.search(a, status.text):
         message['term'] = term
     else:
         pass

      print message['text'], message['term']

这是过滤代码:

setTerms = ['BBC','XFactor','Obama']
streamer.filter(track = setTerms)

它匹配字符串,但我还需要能够匹配所有实例,例如。 BBC 还应与#BBC、@BBC 或 BBC1 等匹配。

所以我的问题是如何在 setTerms 中获得一个术语,例如 BBC,以匹配 if re.search(term, status.text) 中的所有这些实例?

谢谢

【问题讨论】:

标签: python regex twitter tweepy


【解决方案1】:

您是否尝试过将所有搜索词放在一个表达式中?

即setTerms = '(BBC)|(XFactor)|(Obama)',然后查看它是否匹配整个字符串(不仅仅是单个单词?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2011-04-14
    • 2013-08-22
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多