scrapy自定義pipeline類實現(xiàn)將采集數(shù)據(jù)保存到mongodb的方法
更新時間:2015年04月16日 11:32:19 作者:pythoner
這篇文章主要介紹了scrapy自定義pipeline類實現(xiàn)將采集數(shù)據(jù)保存到mongodb的方法,涉及scrapy采集及操作mongodb數(shù)據(jù)庫的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了scrapy自定義pipeline類實現(xiàn)將采集數(shù)據(jù)保存到mongodb的方法。分享給大家供大家參考。具體如下:
# Standard Python library imports
# 3rd party modules
import pymongo
from scrapy import log
from scrapy.conf import settings
from scrapy.exceptions import DropItem
class MongoDBPipeline(object):
def __init__(self):
self.server = settings['MONGODB_SERVER']
self.port = settings['MONGODB_PORT']
self.db = settings['MONGODB_DB']
self.col = settings['MONGODB_COLLECTION']
connection = pymongo.Connection(self.server, self.port)
db = connection[self.db]
self.collection = db[self.col]
def process_item(self, item, spider):
err_msg = ''
for field, data in item.items():
if not data:
err_msg += 'Missing %s of poem from %s\n' % (field, item['url'])
if err_msg:
raise DropItem(err_msg)
self.collection.insert(dict(item))
log.msg('Item written to MongoDB database %s/%s' % (self.db, self.col),
level=log.DEBUG, spider=spider)
return item
希望本文所述對大家的python程序設計有所幫助。
相關文章
淺談Python 命令行參數(shù)argparse寫入圖片路徑操作
這篇文章主要介紹了淺談Python 命令行參數(shù)argparse寫入圖片路徑操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python實現(xiàn)打包成庫供別的模塊調(diào)用
這篇文章主要介紹了Python實現(xiàn)打包成庫供別的模塊調(diào)用,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
python保存log日志,實現(xiàn)用log日志畫圖
今天小編就為大家分享一篇python保存log日志,實現(xiàn)用log日志來畫圖,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python中獲取網(wǎng)頁狀態(tài)碼的兩個方法
這篇文章主要介紹了Python中獲取網(wǎng)頁狀態(tài)碼的兩個方法,分別使用urllib模塊和requests模塊實現(xiàn),需要的朋友可以參考下2014-11-11
解決python使用pd.read_csv()出現(xiàn)錯誤UnicodeDecodeError:?'utf-8&
你是否有過之前用pd.read打開csv文件都正常,但突然有一天運行以前的代碼就突然報錯,這篇文章主要給大家介紹了關于如何解決python使用pd.read_csv()出現(xiàn)錯誤UnicodeDecodeError:?'utf-8'?codec?can't?decode......的相關資料,需要的朋友可以參考下2023-12-12

