目錄
1.一般方法存入MongoDB
2.定義函數(shù)存入MongoDB
3.在scrapy中存入MongoDB
4.從數(shù)據(jù)庫(kù)中取數(shù)據(jù)
正文:
1.一般方法存入MongoDB
import pymongo
from pymongo.collection import Collection????#注意英文拼寫
client= pymongo.MongoClient("localhost")
db= client['xxx']????#xxx為數(shù)據(jù)庫(kù)名
id_Collection= Collection(db,'xxx')????#xxx為表名
id_Collection.insert("{xxx}")????#xxx為字典
2.定義函數(shù)存入MongoDB
import pymongo
def save_to_mongo(self,result):
? ? client= pymongo.MongoClient("xxx")????#聯(lián)接客戶端,本地寫localhost,
? ? db= client["xxx"]????#數(shù)據(jù)庫(kù)名稱盅惜,按自己需求寫一個(gè)
? ? db['xxx'].Insert(result)????#xxx為表名扯再,result為要存的數(shù)據(jù)
3.在scrapy中存入MongoDB
#首先在setting中最下面加
mongo_host= "127.0.0.1"
mongo_port= 27017
mongo_db_name= "xxx"#xxx為數(shù)據(jù)庫(kù)名稱
mongo_db_collection= "xxx" #xxx為表名稱
#然后在pipelines.py中
import pymongo
from xxx.settingsimport mongo_port
class xxxPipeline(object):
? ? def __init__(self):
? ? ? ? host= mongo_host
????????port= mongo_port
????????dbname= mongo_db_name
????????sheetname= mongo_db_collection
????????client= pymongo.MongoClient(host=host,port=port)
????????mydb= client[dbname]
????????self.post= mydb[sheetname]
def process_item(self,item,spider):
? ? ? ? data= dict(item)
????????self.post.insert(data)
????????return item
4.從mongodb數(shù)據(jù)庫(kù)中取數(shù)據(jù)
import pymongo
from pymongo.collectionimport? Collection
client= pymongo.MongoClient("localhost:27017")
db= client['xxx']#xxx為數(shù)據(jù)庫(kù)名
id_collection= Collection(db,'xxx')????#xxx為表名
task_id_collection.find_one_and_delete({})#查找一個(gè)并刪除掉