Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于stringarray中的元素在pymongo中创建名称为集合的MongoDB集合_Python_Arrays_Mongodb_Twitter_Pymongo - Fatal编程技术网

Python 基于stringarray中的元素在pymongo中创建名称为集合的MongoDB集合

Python 基于stringarray中的元素在pymongo中创建名称为集合的MongoDB集合,python,arrays,mongodb,twitter,pymongo,Python,Arrays,Mongodb,Twitter,Pymongo,我是python新手,所以对我放松点!我正试图通过tweepy实现一个流侦听器,该侦听器基于关键字过滤器(使用字符串数组)对推文进行流式处理,并将这些推文保存到mongodb中的集合(使用pymongo) 我已经成功地做到了这一点,但现在我想更进一步,从我的filterKeywords数组中将一条经过特定字符串过滤的tweet保存到一个mongodb集合中,该集合以该数组的string元素命名(即通过字符串元素“Apple”过滤的tweet将保存到名为“Apple”的mongodb集合中 我已经

我是python新手,所以对我放松点!我正试图通过tweepy实现一个流侦听器,该侦听器基于关键字过滤器(使用字符串数组)对推文进行流式处理,并将这些推文保存到mongodb中的集合(使用pymongo)

我已经成功地做到了这一点,但现在我想更进一步,从我的filterKeywords数组中将一条经过特定字符串过滤的tweet保存到一个mongodb集合中,该集合以该数组的string元素命名(即通过字符串元素“Apple”过滤的tweet将保存到名为“Apple”的mongodb集合中

我已经尝试过通过on_data方法中的for循环在数组中循环,如果在tweet中找到元素,则尝试基于该关键字元素创建一个集合,但它只是创建一个名为“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)

我在回答另一个问题时修正了这个问题,我的pymongo代码实际上起了作用,这是为了将推文加载到JSON格式。这需要对“text”键进行“key”检查,然后检查该文本是否包含我的filterKeyword。这是更新的on_数据方法

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)

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