Python爬蟲(chóng)之使用MongoDB存儲(chǔ)數(shù)據(jù)的實(shí)現(xiàn)
MongoDB是一種基于文檔的(Document-Oriented)NoSQL數(shù)據(jù)庫(kù),它設(shè)計(jì)簡(jiǎn)單、易于使用,適用于大多數(shù)應(yīng)用程序開(kāi)發(fā)需求。與傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù)不同,MongoDB不需要一個(gè)明確定義的模式來(lái)存儲(chǔ)數(shù)據(jù),這意味著可以輕松存儲(chǔ)包含不同類型和結(jié)構(gòu)的數(shù)據(jù)。
在Python中使用MongoDB存儲(chǔ)數(shù)據(jù)是非常簡(jiǎn)單的,Python中有很多優(yōu)秀的MongoDB驅(qū)動(dòng)庫(kù)可以選擇,本文將介紹如何使用pymongo驅(qū)動(dòng)庫(kù)來(lái)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)。
1.安裝pymongo
在使用pymongo之前,需要先進(jìn)行安裝。使用pip命令可以快速安裝pymongo,如下所示:
pip install pymongo
2.連接MongoDB數(shù)據(jù)庫(kù)
連接MongoDB數(shù)據(jù)庫(kù)非常簡(jiǎn)單,只需指定MongoDB數(shù)據(jù)庫(kù)的地址和端口號(hào)即可,如下所示:
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")上面的代碼通過(guò)pymongo驅(qū)動(dòng)庫(kù)創(chuàng)建一個(gè)MongoDB客戶端,連接本地的MongoDB數(shù)據(jù)庫(kù),默認(rèn)端口號(hào)為27017。
3.創(chuàng)建數(shù)據(jù)庫(kù)和集合
在MongoDB中,數(shù)據(jù)以文檔的形式存儲(chǔ)在集合(Collection)中,集合是MongoDB中的一種無(wú)結(jié)構(gòu)化的數(shù)據(jù)結(jié)構(gòu),類似于關(guān)系型數(shù)據(jù)庫(kù)中的表。
要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫(kù)和集合,可以使用以下代碼:
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# 創(chuàng)建數(shù)據(jù)庫(kù)
mydb = client["testdb"]
# 創(chuàng)建集合
mycol = mydb["testcol"]在這個(gè)例子中,數(shù)據(jù)庫(kù)名為“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()方法來(lái)查詢MongoDB中的數(shù)據(jù)。find()方法可以接受查詢條件作為參數(shù),如果沒(méi)有查詢條件,則返回集合中的所有文檔。
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)在上面的代碼中,第一個(gè)for循環(huán)查詢了集合中的所有文檔,第二個(gè)for循環(huán)查詢了符合條件的文檔。
6.更新數(shù)據(jù)
使用update_one()方法或update_many()方法來(lái)更新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()方法來(lái)刪除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存儲(chǔ)數(shù)據(jù)非常方便,只需安裝pymongo驅(qū)動(dòng)庫(kù)即可。通過(guò)連接MongoDB數(shù)據(jù)庫(kù)、創(chuàng)建數(shù)據(jù)庫(kù)和集合、插入、查詢、更新和刪除數(shù)據(jù)等基本操作,可以輕松實(shí)現(xiàn)數(shù)據(jù)的存儲(chǔ)和管理。
到此這篇關(guān)于Python爬蟲(chóng)之使用MongoDB存儲(chǔ)數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python MongoDB存儲(chǔ)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python生成動(dòng)態(tài)路由軌跡圖的示例詳解
在當(dāng)今的數(shù)據(jù)驅(qū)動(dòng)時(shí)代,可視化技術(shù)在數(shù)據(jù)分析和決策支持中扮演著越來(lái)越重要的角色,本文將介紹如何使用Python來(lái)生成動(dòng)態(tài)的路由軌跡圖,需要的可以了解下2025-02-02
jupyter notebook使用argparse傳入list參數(shù)
這篇文章主要介紹了jupyter notebook使用argparse傳入list參數(shù),jupyter notebook其實(shí)是可以使用 argparse來(lái)調(diào)用參數(shù)的,只要把參數(shù)轉(zhuǎn)為list即可,下面來(lái)看看具體的實(shí)現(xiàn)過(guò)程吧2022-01-01
Python實(shí)現(xiàn)在Excel中動(dòng)態(tài)插入與刪除圖片的完整指南
在自動(dòng)化報(bào)表生成、數(shù)據(jù)可視化或辦公流程優(yōu)化中,經(jīng)常需要在 Excel 文件中嵌入圖表,本文將詳細(xì)介紹如何使用主流庫(kù)在 Excel 中添加圖片和刪除圖片,有需要的小伙伴可以了解下2026-01-01
在Python中處理日期和時(shí)間的基本知識(shí)點(diǎn)整理匯總
這篇文章主要介紹了在Python中處理日期和時(shí)間的基本知識(shí)點(diǎn)整理匯總,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
Pycharm 使用 Pipenv 新建的虛擬環(huán)境(圖文詳解)
pipenv 是 Pipfile 主要倡導(dǎo)者、requests 作者 Kenneth Reitz 寫(xiě)的一個(gè)命令行工具,主要包含了Pipfile、pip、click、requests和virtualenv。這篇文章主要介紹了Pycharm 使用 Pipenv 新建的虛擬環(huán)境的問(wèn)題,需要的朋友可以參考下2020-04-04
編譯 pycaffe時(shí)報(bào)錯(cuò):fatal error: numpy/arrayobject.h沒(méi)有那個(gè)文件或目錄
這篇文章主要介紹了編譯 pycaffe時(shí)報(bào)錯(cuò):fatal error: numpy/arrayobject.h沒(méi)有那個(gè)文件或目錄,需要的朋友可以參考下2020-11-11

