import requests
import pandas as pd
from   oneclass.onedb import Onedb
onedb=Onedb()
 
ttlpe=pd.DataFrame()
url = 'http://27.push2.eastmoney.com/api/qt/clist/get'
ttlqty=0
for i in range(1, 100):
    data = {
        #'fields': 'f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152',
        'fields': 'f2,f9,f12,f14,f20,f21,f23',
        'pz': 1000,         # 每页条数
        'pn': i,            # 页码
        'fs': 'm:0 t:6,m:0 t:80,m:1 t:2,m:1 t:23,m:0 t:81 s:2048'
    }
    response = requests.get(url, data)
    response_json = response.json()
    #print(i, response_json)
    # 返回数据为空时停止循环
    print(i, response_json)
    print("\n")
    if response_json['data'] is None:
        break
    for j, k in response_json['data']['diff'].items():
        code = k['f12']         # 代码
        if code[0:1]=="6":
            code=code+".SH"
        elif code[0:1]=="8" or code[0:1]=="4":
            code=code+".BJ"
        else:
            code=code+".SZ"


        name = k['f14']         # 名称
        price = k['f2']         # 股价
        pe = k['f9']            # 动态市盈率
        pb = k['f23']           # 市净率
        total_value = k['f20']          # 总市值
        currency_value = k['f21']       # 流通市值
        price = round(price/100, 2)     # 价格转换为正确值（保留2位小数）
        pe = round(pe/100, 2)           # 市盈率转换为正确值（保留2位小数）
        pb = round(pb/100, 2)           # 市净率转换为正确值（保留2位小数）
        total_value = round(total_value / 100000000, 2)         # 总市值转换为亿元（保留2位小数）
        currency_value = round(currency_value / 100000000, 2)   # 流通市值转换为亿元（保留2位小数）
        #print('代码: %s, 名称: %s, 现价: %s, 动态市盈率: %s, 市净率: %s, 总市值: %s亿, 流通市值: %s')
        ttlqty+=1
        #print(f'{ttlqty}:{code} | {name} | {price} | {pe} | {pb}')
        #tmppe={'stockcode':code,'stockname':name,'price':price,'pe':pe,'pb':pb}
        tmppe={'stockcode':code,'stockname':name,'price':price,'pe':pe,'pb':pb,'TV':total_value,'CV':currency_value}
        ttlpe=pd.concat([ttlpe, pd.DataFrame([tmppe])], ignore_index=True)

#print(ttlpe)
if ttlqty>5000:
    onedb.execute_sql("DROP TABLE IF EXISTS `pe`")
    onedb.into_table(ttlpe,"pe","stockcode",9)
    print(ttlqty)
else:
  print("数据数量小于5000不正常，没有更新数据库")
  print(ttlqty)
