Python操作MongoDB


安裝 pymongo 模塊

  conda/pip install pymongo

使用步驟

  創建數據庫連接

    pymongo.MongoClient(host='localhost', port=27017)

      本地ip:localhost

      默認端口:27017

  獲得數據庫對象

    數據庫連接.數據庫名稱

  獲得 collection 對象

    數據庫名稱.collection名稱

 

爬取豆瓣Top250信息並保存到MongoDB

 1 # -*- coding: utf-8 -*-
 2 import pymongo
 3 
 4 # Define your item pipelines here
 5 #
 6 # Don't forget to add your pipeline to the ITEM_PIPELINES setting
 7 # See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
 8 
 9 
10 class ScrapyDoubanMongoPipeline(object):
11 
12     def open_spider(self, spider):
13         self.conn = pymongo.MongoClient(host='localhost', port=27017)
14         self.db = self.conn.douban_movie
15         # self.db = self.conn['douban_movie']
16         self.movies = self.db.movies
17 
18     def process_item(self, item, spider):
19         self.movies.insert(
20             {
21                 "title":item['title'],
22                 "score":item['score'],
23                 "rank":item['rank'],
24                 "abstract":item['abstract'],
25                 "describe":item['describe'],
26             }
27         )
28 
29     def close_spider(self, spider):
30         self.conn.close()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM