折腾:
【未解决】PySpider中把结果保存到MongoDB数据库中
期间,涉及到本地开发测试和在线生产等不同环境,传递不同参数给pymongo的MongoClient,去打开和连接MongoDB。
而关于mongodb的uri,此处希望可以根据参数不同自动生成。
参考之前用过的代码,另外再去整合
先参考:
经过调试,目前用代码:
from urllib.parse import quote_plus def generateMongoUri(host=None, port=None, isUseAuth=False, username=None, password=None, authSource=None, authMechanism=None): """"generate mongodb uri""" mongodbUri = "" if not host: # host = "127.0.0.0" host = "localhost" if not port: port = 27017 mongodbUri = "mongodb://%s:%s" % ( host, \ port ) # ' mongodb://localhost:27017 ' # ' mongodb://xxx:27017 ' if isUseAuth: mongodbUri = "mongodb://%s:%s@%s:%s" % ( quote_plus(username), \ quote_plus(password), \ host, \ port \ ) print(mongodbUri) if authSource: mongodbUri = mongodbUri + ("/%s" % authSource) print("mongodbUri=%s" % mongodbUri) if authMechanism: mongodbUri = mongodbUri + ("?authMechanism=%s" % authMechanism) print("mongodbUri=%s" % mongodbUri) print("return mongodbUri=%s" % mongodbUri) # mongodb://username:quoted_password@host:port/authSource?authMechanism=authMechanism # mongodb://localhost:27017 return mongodbUri if __name__ == "__main__": # test default defaultMongoUri = generateMongoUri() print("defaultMongoUri=%s" % defaultMongoUri) # test normal host port normalMongoUri = generateMongoUri(host="1.2.3.4", port=34567) print("normalMongoUri=%s" % normalMongoUri) # test use auth useAuthMongoUri = generateMongoUri(host="1.2.3.4", port=34567, isUseAuth=True, username="normal_user", password="complexP@D", authSource="admin", authMechanism="SCRAM-SHA-1") print("useAuthMongoUri=%s" % useAuthMongoUri)
输出:
return mongodbUri= mongodb://localhost:27017 defaultMongoUri= mongodb://localhost:27017 return mongodbUri= mongodb://1.2.3.4:34567 normalMongoUri= mongodb://1.2.3.4:34567 mongodb://normal_user:complexP%[email protected]:34567 mongodbUri= mongodb://normal_user:complexP%[email protected]:34567/admin mongodbUri= mongodb://normal_user:complexP%[email protected]:34567/admin?authMechanism=SCRAM-SHA-1 return mongodbUri= mongodb://normal_user:complexP%[email protected]:34567/admin?authMechanism=SCRAM-SHA-1 useAuthMongoUri= mongodb://normal_user:complexP%[email protected]:34567/admin?authMechanism=SCRAM-SHA-1 [1] 4243 terminated env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /usr/local/bin/python3
貌似满足需求了。