[英]Storing tweets in MongoDB using pymongo
I am trying to save tweets directly from Twitter
into MongoDB
for retrieval later, but I keep getting error messages with regards to json
. 我正在尝试将Twitter
推文直接保存到MongoDB
以供日后检索,但是我不断收到有关json
错误消息。 Someone please help! 有人请帮忙! Here is the sample codes with error messages. 这是带有错误消息的示例代码。
searcher = keywordSearcher('','','') ## this is an object class
client = MongoClient('localhost', 27017)
db = client.test_dbase ## connect to database twitter
collection = db.rio_olympics ## create a collection object
result = searcher.getTwitterComment('Olympics 2016', '21 August 2016', 2) ## this is where I query Twitter Search API
py_dict = json.load(result)
post_id = collection.insert_many(py_dict)
error message: 错误信息:
Traceback (most recent call last):
File "/home/edidiong/myWorkSpace/keywordSearchTest.py", line 12, in <module>
class keywordSearchTest:
File "/home/edidiong/myWorkSpace/keywordSearchTest.py", line 45, in keywordSearchTest
py_dict = json.load(result)
File "/usr/lib/python2.7/json/__init__.py", line 287, in load
return loads(fp.read(),
AttributeError: 'dict' object has no attribute 'read'
Your searcher.getTwitterComment
returns a dictionary and not json. 您的searcher.getTwitterComment
返回一个字典,而不是json。
That is why you get this error, json.load()
is already called probably. 这就是为什么您会收到此错误的原因,可能已经调用了json.load()
。
The error message indicates you are trying to load
a dictionary object. 错误消息表明您正在尝试load
字典对象。 This means you already have a dictionary object. 这意味着您已经有一个字典对象。 I'm not sure what the point of the json.load
would be even if it did work. 我不确定json.load
的意义是什么,即使它确实起作用了。
So you can probably just do: 因此,您可能可以执行以下操作:
py_dict = searcher.getTwitterComment("Olympics 2016", '21 August 2016', 2)
post_id = collection.insert_many(py_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.