使用基于字符串数组中元素的集合名称在 pymongo 中创建一个 MongoDB 集合
Creating a MongoDB collection in pymongo with the name of the collection based on an element from a stringarray
我是 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)
我从我问的另一个问题的答案中解决了这个问题,我的 pymongo 代码实际上起作用了,它与我将推文加载为 JSON 格式有关。然后,这需要 'key' 检查 'text' 键,然后检查该文本是否包含我的 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)
我是 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)
我从我问的另一个问题的答案中解决了这个问题,我的 pymongo 代码实际上起作用了,它与我将推文加载为 JSON 格式有关。然后,这需要 'key' 检查 'text' 键,然后检查该文本是否包含我的 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)