使用python連線mongodb查詢指定資料爬取用txt or excel儲存
阿新 • • 發佈:2018-11-23
import pymongo
import csv
import json
import sys
import importlib
importlib.reload(sys)
import xlwt
from pymongo import MongoClient
該導的先導進來
mongo_url = "172.28.9.62:1234"
# mongodb服務的地址和埠號
url引數
client = pymongo.MongoClient(mongo_url)
獲取client
DATABASE = "test" #連線到資料庫test db = client[DATABASE] db.collection_names() 顯示子集名稱
COLLECTION = "microblogs"
db_coll = db[COLLECTION ]
子資料集
queryArgs = {'geo':{'$ne':None}}
projectionFields = {'geo':True,'text':True,'user_id':True,'created_at':True}
searchRes = db_coll.find(queryArgs, projection = projectionFields)
接下來是查詢操作,find(條件,返回值)
寫入excel和txt特輯: