Python爬蟲之使用MongoDB存儲數(shù)據(jù)的實現(xiàn)
MongoDB是一種基于文檔的(Document-Oriented)NoSQL數(shù)據(jù)庫,它設計簡單、易于使用,適用于大多數(shù)應用程序開發(fā)需求。與傳統(tǒng)的關系型數(shù)據(jù)庫不同,MongoDB不需要一個明確定義的模式來存儲數(shù)據(jù),這意味著可以輕松存儲包含不同類型和結(jié)構(gòu)的數(shù)據(jù)。
在Python中使用MongoDB存儲數(shù)據(jù)是非常簡單的,Python中有很多優(yōu)秀的MongoDB驅(qū)動庫可以選擇,本文將介紹如何使用pymongo驅(qū)動庫來實現(xiàn)數(shù)據(jù)存儲。
1.安裝pymongo
在使用pymongo之前,需要先進行安裝。使用pip命令可以快速安裝pymongo,如下所示:
pip install pymongo
2.連接MongoDB數(shù)據(jù)庫
連接MongoDB數(shù)據(jù)庫非常簡單,只需指定MongoDB數(shù)據(jù)庫的地址和端口號即可,如下所示:
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")上面的代碼通過pymongo驅(qū)動庫創(chuàng)建一個MongoDB客戶端,連接本地的MongoDB數(shù)據(jù)庫,默認端口號為27017。
3.創(chuàng)建數(shù)據(jù)庫和集合
在MongoDB中,數(shù)據(jù)以文檔的形式存儲在集合(Collection)中,集合是MongoDB中的一種無結(jié)構(gòu)化的數(shù)據(jù)結(jié)構(gòu),類似于關系型數(shù)據(jù)庫中的表。
要創(chuàng)建一個數(shù)據(jù)庫和集合,可以使用以下代碼:
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# 創(chuàng)建數(shù)據(jù)庫
mydb = client["testdb"]
# 創(chuàng)建集合
mycol = mydb["testcol"]在這個例子中,數(shù)據(jù)庫名為“testdb”,集合名為“testcol”。
4.插入數(shù)據(jù)
要向MongoDB中插入數(shù)據(jù),可以使用insert_one()和insert_many()方法。insert_one()方法插入一條數(shù)據(jù),insert_many()方法插入多條數(shù)據(jù)。
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["testdb"]
mycol = mydb["testcol"]
# 插入一條數(shù)據(jù)
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)
# 插入多條數(shù)據(jù)
mylist = [
{ "name": "Amy", "address": "Apple st 652" },
{ "name": "Hannah", "address": "Mountain 21" },
{ "name": "Michael", "address": "Valley 345" },
{ "name": "Sandy", "address": "Ocean blvd 2" },
{ "name": "Betty", "address": "Green Grass 1" },
{ "name": "Richard", "address": "Sky st 331" },
{ "name": "Susan", "address": "One way 98" },
{ "name": "Vicky", "address": "Yellow Garden 2" },
{ "name": "Ben", "address": "Park Lane 38" },
{ "name": "William", "address": "Central st 954" },
{ "name": "Chuck", "address": "Main Road 989" },
{ "name": "Viola", "address": "Sideway 1633"}
]
x = mycol.insert_many(mylist)5.查詢數(shù)據(jù)
使用find()方法來查詢MongoDB中的數(shù)據(jù)。find()方法可以接受查詢條件作為參數(shù),如果沒有查詢條件,則返回集合中的所有文檔。
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["testdb"]
mycol = mydb["testcol"]
# 查詢所有數(shù)據(jù)
for x in mycol.find():
print(x)
# 查詢name為John的數(shù)據(jù)
myquery = { "name": "John" }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)在上面的代碼中,第一個for循環(huán)查詢了集合中的所有文檔,第二個for循環(huán)查詢了符合條件的文檔。
6.更新數(shù)據(jù)
使用update_one()方法或update_many()方法來更新MongoDB中的數(shù)據(jù)。
import pymongo client = pymongo.MongoClient(“mongodb://localhost:27017/”) mydb = client[“testdb”] mycol = mydb[“testcol”]
更新一條數(shù)據(jù)
myquery = { “name”: “John” }
newvalues = { “$set”: { “address”: “Canyon 123” } }
mycol.update_one(myquery, newvalues)更新多條數(shù)據(jù)
myquery = { “address”: { “regex": "^S" } } newvalues = { "set”: { “name”: “Minnie” } }
x = mycol.update_many(myquery, newvalues)
print(x.modified_count, “文檔已修改”)7.刪除數(shù)據(jù)
使用delete_one()方法或delete_many()方法來刪除MongoDB中的數(shù)據(jù)。
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["testdb"]
mycol = mydb["testcol"]
# 刪除一條數(shù)據(jù)
myquery = { "address": "Mountain 21" }
mycol.delete_one(myquery)
# 刪除多條數(shù)據(jù)
myquery = { "name": {"$regex": "^S"} }
x = mycol.delete_many(myquery)
print(x.deleted_count, "文檔已刪除")總結(jié):
在Python中使用MongoDB存儲數(shù)據(jù)非常方便,只需安裝pymongo驅(qū)動庫即可。通過連接MongoDB數(shù)據(jù)庫、創(chuàng)建數(shù)據(jù)庫和集合、插入、查詢、更新和刪除數(shù)據(jù)等基本操作,可以輕松實現(xiàn)數(shù)據(jù)的存儲和管理。
到此這篇關于Python爬蟲之使用MongoDB存儲數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關Python MongoDB存儲數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Python保存網(wǎng)頁上的圖片或者保存頁面為截圖
這篇文章主要介紹了使用Python保存網(wǎng)頁上的圖片或者保存頁面為截圖的方法,保存網(wǎng)頁圖片主要用到urllib模塊,即簡單的爬蟲原理,需要的朋友可以參考下2016-03-03
Pytorch根據(jù)layers的name凍結(jié)訓練方式
今天小編就為大家分享一篇Pytorch根據(jù)layers的name凍結(jié)訓練方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
在Python中append以及extend返回None的例子
今天小編就為大家分享一篇在Python中append以及extend返回None的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07

