代码拉取完成,页面将自动刷新
import pymongo
# MongoDB connection details
mongo_url = "mongodb://localhost:27017/"
db_name = "steam"
collection_name = "Items"
# Save data in the database
def save_data(data):
client = pymongo.MongoClient(mongo_url)
# print(client.list_database_names())
db = client[db_name]
collection = db[collection_name]
documents = collection.find()
for item in data:
item["_id"] = item["name"] # Set the item name as the _id
# Check if the item already exists in the database
if collection.count_documents({"_id": item["_id"]}) == 0:
collection.insert_one(item)
else:
collection.update_one({"_id": item["_id"]}, {"$set": item})
client.close()
# Select all documents from the "Items" table
def select_all():
client = pymongo.MongoClient(mongo_url)
db = client[db_name]
collection = db[collection_name]
documents = collection.find()
for document in documents:
print(document)
client.close()
# Example usage
# data = [
# {"name": "item1", "price": 56},
# {"name": "item2", "price": 20},
# {"name": "item4", "price": 30}
# ]
# save_data(data)
# select_all()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。