【爬虫】-003-MongoDB入门

2017-12-26  本文已影响14人  9756a8680596
  1. 简单的例子
#coding: utf-8
from pymongo import MongoClient

host = 'localhost'
port = 27017

client = MongoClient(host, port)    #  访问服务器和对应端口,创建一个connection对象
db = client['test']                 #   创建名为test的数据库,也可以写成 db = client.test
sheet = db['sheet']                 #   在 数据库 中 创建名为sheet的表,也可以写成 sheet = db.sheet
for i in range(101):
    print(i)
    sheet.insert_one({  #
            'name': 'name' + str(i),
            'age': i,
        })
  1. 数据导入和导出

参考资料:
-1. Python下MongoDB入门学习. http://henan.leanote.com/post/Python%E4%B8%8BMongoDB%E5%85%A5%E9%97%A8%E5%AD%A6%E4%B9%A0

上一篇下一篇

猜你喜欢

热点阅读