滬深A(yù)股數(shù)據(jù)每日獲取

使用Python實現(xiàn)

import requests
import xlsxwriter
import time

def creatlist(item):
    temp = []
    temp.append(item['name'])#股票名稱
    temp.append("=TEXT(" + item['code'] + ",\"000000\")") #股票代碼
    temp.append(float(item['buy'])) #最新價
    temp.append("=TEXT(" + str(round(float(item['changepercent'])/100,4)) + ",\"0.00%\")")#跌漲幅
    temp.append(item['pricechange'])#跌漲額
    temp.append(item['volume']) #成交量
    temp.append(item['amount']) #成交額
    temp.append(item['mktcap']*10000)#總市值
    temp.append(item['nmc']*10000)#流通市值
    temp.append(item['pb'])#市盈率
    temp.append(item['per'])#市凈率
    temp.append("=TEXT(" + str(round(float(item['turnoverratio'])/100,4)) + ",\"0.00%\")")#換手率
    temp.append(float(item['trade']))#買入
    temp.append(float(item['sell'])) #賣出
    temp.append(float(item['high']))#最高
    temp.append(float(item['low']))#最低
    temp.append(float(item['open']))#今開
    temp.append(float(item['settlement']))#昨收
    return temp

try:
    abc = "http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeStockCount?node=hs_a"
    sum = requests.get(abc).json()
    timenow = time.strftime("%Y%m%d%H%M%S", time.localtime())
    workbook = xlsxwriter.Workbook("hushenAstockexchange" + timenow + ".xlsx")
    worksheet = workbook.add_worksheet()

    namelist = ['股票名稱', '股票代碼', '最新價','跌漲幅','跌漲額','成交量','成交額','總市值','流通市值','市盈率','市凈率','換手率','買入','賣出','最高','最低','今開','昨收']
    worksheet.write_row(0, 0, namelist)
    for i in range(1,int(int(sum)//100 + 2)):
        url_temp = "http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=" + str(i) + "&num=100&sort=changepercent&asc=0&node=hs_a&symbol="
        res_temp = requests.get(url_temp).json()
        for j in range(1,len(res_temp)+1):
            my_list = creatlist(res_temp[j-1])
            worksheet.write_row(j+(i-1)*100, 0, my_list)
    workbook.close()
except Exception as e:
    print(e)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容