【问题标题】:Creating a MongoDB collection in pymongo with the name of the collection based on an element from a stringarray在 pymongo 中使用基于字符串数组中的元素的集合名称创建 MongoDB 集合
【发布时间】:2015-04-19 17:14:06
【问题描述】:

我是 python 新手,所以放轻松!我正在尝试通过 tweepy 实现一个流侦听器,它基于关键字过滤器(为此使用字符串数组)流式传输推文,并将这些推文保存到 mongodb 中的集合中(使用 pymongo)。

我已经成功地做到了这一点,但现在我想通过将由我的 filterKeywords 数组中的特定字符串过滤的推文保存到以它被过滤的数组的字符串元素命名的 mongodb 集合来进一步实现这一点(即一条由字符串元素“Apple”过滤的推文,将保存到名为“Apple”的 mongodb 集合中。

我已经尝试通过 on_data 方法中的 for 循环循环遍历数组和如果在推文中找到一个元素然后尝试基于该关键字元素创建一个集合但它只是创建一个名为“word”的集合并将其保存到该集合中。

下面是我的代码(出于显而易见的原因,我省略了我的 twitter 身份验证凭据)。希望有人能帮忙

import tweepy
import pymongo
import json

consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)

filterKeywords = ['IBM', 'Microsoft', 'Facebook', 'Twitter', 'Apple', 'Google', 'Amazon', 'EBay', 'Diageo',
                   'General Motors', 'General Electric', 'Telefonica', 'Rolls Royce', 'Walmart', 'HSBC', 'BP',
                   'Investec', 'WWE', 'Time Warner', 'Santander Group']


class CustomStreamListener(tweepy.StreamListener):
    def __init__(self, api):
        self.api = api
        super(tweepy.StreamListener, self).__init__()

        self.db = pymongo.MongoClient().mydb


    def on_data(self, tweet):
        data = json.loads(tweet)
        for word in filterKeywords:
            if word in data:
                collection = self.db[word]
                collection.insert(data),
        print (tweet)

    def on_error(self, status_code):
        return True # Don't kill the stream

    def on_timeout(self):
        return True # Don't kill the stream


sapi = tweepy.streaming.Stream(auth, CustomStreamListener(api))
sapi.filter(track=filterKeywords)

【问题讨论】:

  • db[word] 这样的东西word 是一个值为"IBM" 的变量将访问一个名为IBM 的集合,这对我来说很好。如果作为测试,您将 word 替换为像 "cookies" 这样的字符串文字会发生什么?
  • 我也是这么想的,但它对我没用。我还尝试用字符串替换它(使用你建议的“cookies”),现在它甚至没有创建任何集合,即使我将代码恢复到上面。我在这里想念的东西一定很简单。我会再看看为什么我的代码停止创建集合句号
  • 好吧,在我看来,我的 if 语句根本不起作用,当我删除它并使用建议的字符串文字时它起作用 def on_data(self, tweet): data = json.loads(tweet) collection = db["cookies"] collection.insert(data), print (tweet) 有没有一种简洁的方法来做到这一点,而不是写20个案例陈述?我只是认为这是一种糟糕的编码方式,这意味着如果需要进行任何更改,我可能必须进行 20 次而不是一次更改。
  • 是不是 if 语句没有按您期望的方式工作,也许?你应该能够像你想要的那样动态地做事情。 data 的值是多少?
  • 是的,看起来就是这样。 data 的值将是从 tweepy 流侦听器中提取的当前 json 对象,因此每次从流中提取新推文时它都会更改。

标签: python arrays mongodb twitter pymongo


【解决方案1】:

我已经从我问的另一个问题的答案中解决了这个问题,我的 pymongo 代码实际上可以正常工作,这与我将推文加载为 JSON 格式有关。然后,这需要对“文本”键进行“键”检查,然后检查该文本是否包含我的 filterKeyword 之一。这是更新后的 on_data 方法

def on_data(self, data):
datajson = json.loads(data)
for word in filterKeywords:
    if datajson.get('text') and word in datajson['text']:
        collection = db[word]
        collection.insert(datajson)
        print('Tweet found filtered by ' + word)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2014-09-02
    • 2020-08-08
    • 2019-02-06
    • 2020-09-22
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多