折腾:
【未解决】PySpider中把结果保存到MongoDB数据库中
期间,涉及到本地开发测试和在线生产等不同环境,传递不同参数给pymongo的MongoClient,去打开和连接MongoDB。
而关于mongodb的uri,此处希望可以根据参数不同自动生成。
参考之前用过的代码,另外再去整合
先参考:
经过调试,目前用代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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 host, \ port ) # ' mongodb: / / localhost: 27017 ' # ' mongodb: / / xxx: 27017 ' if isUseAuth: 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) |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 % 40D @ 1.2 . 3.4 : 34567 mongodbUri = mongodb: / / normal_user:complexP % 40D @ 1.2 . 3.4 : 34567 / admin mongodbUri = mongodb: / / normal_user:complexP % 40D @ 1.2 . 3.4 : 34567 / admin?authMechanism = SCRAM - SHA - 1 return mongodbUri = mongodb: / / normal_user:complexP % 40D @ 1.2 . 3.4 : 34567 / admin?authMechanism = SCRAM - SHA - 1 useAuthMongoUri = mongodb: / / normal_user:complexP % 40D @ 1.2 . 3.4 : 34567 / admin?authMechanism = SCRAM - SHA - 1 [ 1 ] 4243 terminated env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" / usr / local / bin / python3 |
貌似满足需求了。