import pymongo
# 打开数据库
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
# 若没有Database 和Collection,则会自动创建
mongo_db = mongo_client['SinaData']
# # 若没有Database 和Collection,则会自动创建
mongo_collection = mongo_db['SinaAllStockName']
def FindAllStock():
# 查询所有数据
mydoc = mongo_collection.find()
Allcode = []
Allname = []
for x in mydoc:
# print(x['code'])
# print(x['name'])
Allcode.append(x['code'])
Allname.append(x['name'])
return Allcode, Allname
# 通过汗水代码时间查找收盘价格 有价格,对于无价格的如何处理
def shwoClosePrice(allStock, Fdate):
print('print all stocks price...')
EverStockPrice = []
# 查找一个股票价格
for numE in range(len(allStock)):
mongo_price = mongo_db[allStock[numE]]
FeverStockPrice = mongo_price.find_one({"date": Fdate}) # 查找停牌等问题
if FeverStockPrice is None:
EverStockPrice.append(999999) # 用999999进行站位
else:
EverStockPrice.append(FeverStockPrice['close'])
return EverStockPrice
def main():
print("main...")
allcode, allname = FindAllStock() # 数据库查找所有股票代码、名称
print(allcode)
print(allname)
print(len(allcode)) # 股票数量
print(len(allname))
AllStockClosePrice = shwoClosePrice(allcode, "2020-01-02") # 所有股票股价
print(AllStockClosePrice)
print(len(AllStockClosePrice))
# 明确股票价格列表类型
# print(type(AllStockClosePrice[0])) # <class 'float'>
# print(type(AllStockClosePrice[1])) # <class 'float'>
# 两个列表合并为元组
# NameAndPrice = list(zip(allcode, AllStockClosePrice))
# print(NameAndPrice)
# print(type(NameAndPrice[0][0]))
# print(type(NameAndPrice[0][1]))
# 对股价由低到高排序
# 1.创建一个字典
Code_Price_dict = {allcode[k]: AllStockClosePrice[k] for k in range(len(AllStockClosePrice))}
print(Code_Price_dict)
# 2.使用zip方法将学生字典转换为(value, key)的元组格式
Code_Price_tuplelist = list(zip(Code_Price_dict.values(),
Code_Price_dict.keys()))
# 3.使用sorted函数对元组列表student_list由分数高低进行排序
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
Code_Price_sorted = sorted(Code_Price_tuplelist, reverse=True)
print(Code_Price_sorted)
print(len(Code_Price_sorted))
if __name__ == '__main__':
main()
```python
import pymongo
from datetime import datetime, timedelta
打开数据库
mongo_client = pymongo.MongoClient(‘mongodb://localhost:27017/’)
若没有Database 和Collection,则会自动创建
mongo_db = mongo_client[‘SinaData’]
# 若没有Database 和Collection,则会自动创建
mongo_collection = mongo_db[‘SinaAllStockName’]
def INDEcreas(date, dayNUM):
date = datetime.strptime(date, “%Y-%m-%d”)
modified_date = date + timedelta(days=dayNUM)
datetime.strftime(modified_date, “%Y-%m-%d”)
dateINDE = modified_date.strftime(“%Y-%m-%d”)
return dateINDE
def FindAllStock():
# 查询所有数据
mydoc = mongo_collection.find()
Allcode = []
Allname = []
for x in mydoc:
# print(x[‘code’])
# print(x[‘name’])
Allcode.append(x[‘code’])
Allname.append(x[‘name’])
return Allcode, Allname
通过汗水代码时间查找收盘价格 有价格,对于无价格的如何处理
def shwoClosePrice(allStock, Fdate):
print(‘print all stocks price…’)
EverStockPrice = []
# 查找一个股票价格
for numE in range(len(allStock)):
# print(allStock[numE])
print(‘查找结果: {:.2%}’.format(numE / 4942)) # for 循环为啥不输出呢
# print(str(numE))
mongo_price = mongo_db[allStock[numE]]
# FeverStockPrice = mongo_price.find_one({"date": Fdate}) # 查找停牌等问题
# if FeverStockPrice is None:
# EverStockPrice.append(999999) # 用999999进行站位
# else:
# EverStockPrice.append(FeverStockPrice['close'])
whileNum = 0
while True:
whileNum = whileNum + 1
# code block
s = Fdate
FeverStockPrice = mongo_price.find_one({"date": s}) # 查找停牌等问题
s = INDEcreas(s, -1)
# break out of the loop
if FeverStockPrice:
EverStockPrice.append(FeverStockPrice['close'])
break
if whileNum > 100:
EverStockPrice.append(999999)
break
return EverStockPrice
def main():
print(“main…”)
allcode, allname = FindAllStock() # 数据库查找所有股票代码、名称
print(allcode)
print(allname)
print(len(allcode)) # 股票数量
print(len(allname))
AllStockClosePrice = shwoClosePrice(allcode, "2020-01-02") # 所有股票股价
print(AllStockClosePrice)
print(len(AllStockClosePrice))
# 'bj871970', 'bj871981', 'bj872925', 'bj873122', 'bj873169', 'bj873223'] 比如新的创业板股票交易日期都很新
# 对股价由低到高排序
# 1.创建一个字典
Code_Price_dict = {allcode[k]: AllStockClosePrice[k] for k in range(len(AllStockClosePrice))}
print(Code_Price_dict)
# 2.使用zip方法将学生字典转换为(value, key)的元组格式
Code_Price_tuplelist = list(zip(Code_Price_dict.values(),
Code_Price_dict.keys()))
# 3.使用sorted函数对元组列表student_list由分数高低进行排序
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
Code_Price_sorted = sorted(Code_Price_tuplelist, reverse=True)
print(Code_Price_sorted)
print(len(Code_Price_sorted))
if name == ‘main’:
main()