M101P:参加本课程,运行此文件时出现此错误。有什么建议?

时间:2017-06-10 19:05:20

标签: mongodb mongodb-query

enter image description here

我正在从Mongodb大学学习这门课程。我已在他们的讨论板上发布但未成功。不幸的是,随后的讲座都是基于read_reddit.py运行的。任何帮助将不胜感激!

代码" read_reddit.py"以下是:

import json
import urllib2
import pymongo

# connect to mongo
connection = pymongo.MongoClient("mongodb://localhost")

# get a handle to the reddit database
db = connection.reddit
stories = db.stories

# drop existing collection
stories.drop()

# get the reddit home page
reddit_page = urllib2.urlopen("http://www.reddit.com/r/technology/.json")

# parse the json into python objects
parsed = json.loads(reddit_page.read())

# iterate through every news item on the page
for item in parsed['data']['children']:
    # put it in mongo
    stories.insert_one(item['data'])

1 个答案:

答案 0 :(得分:1)

您需要在脚本中安装您正在使用的python库。在终端中运行以下命令:

pip install urllib2
pip install pymongo