以某股票为例,调用自定义的一个类,读取数据。
class BigAData:
# 获取资金流向数据
def get_money_flow(self, stock_code, page=1, num=20, sort='opendate', asc=0):
'''
该函数通过股票代码从新浪财经API获取资金流向数据。参数包括股票代码、页数、每页数量、排序字段及升序标志。以JSON格式返回数据或在请求失败时返回None。
stock_code: 股票代码
page: 页码
num: 每页数据量
sort: 排序字段
asc: 排序方式
'''
url = f'https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/MoneyFlow.ssl_qsfx_zjlrqs?page={page}&num={num}&sort={sort}&asc={asc}&daima={stock_code}'
# 获取数据
resp = requests.get(url)
# 返回数据
if resp.status_code == 200:
return resp.json()
else:
return None
import pandas as pd
from bad import BigAData
# 获取数据
bad = BigAData()
json = bad.get_money_flow('sz002231')
# 转换为DataFrame
df = pd.DataFrame(json)
print(df)
opendate trade changeratio turnover netamount ratioamount \
0 2024-08-23 4.5100 0.0296804 2382.92 37363281.0000 0.118193
1 2024-08-22 4.3800 0.100503 362.524 42948385.0000 0.930168
2 2024-08-21 3.9800 0.00251889 179.015 -3115710.0000 -0.147867
3 2024-08-20 3.9700 -0.0197531 240.578 -3530293.0100 -0.124667
4 2024-08-19 4.0500 -0.0169903 352.25 -6396122.7800 -0.150614
5 2024-08-16 4.1200 0.00487805 427.51 7383940.1800 0.141605
6 2024-08-15 4.1000 0.0173697 439.131 7624788.0800 0.144833
7 2024-08-14 4.0300 0.0151134 300.415 5807306.0000 0.162652
8 2024-08-13 3.9700 0.0127551 208.087 374879.0000 0.0155296
9 2024-08-12 3.9200 -0.0224439 260.522 -3826773.5000 -0.126509
10 2024-08-09 4.0100 0 323.677 1130124.0000 0.029524
11 2024-08-08 4.0100 -0.0337349 452.193 -7898189.0000 -0.146874
12 2024-08-07 4.1500 0.0453401 654.428 16418773.1200 0.207017
13 2024-08-06 3.9700 0.0284974 328.024 1869527.8200 0.0491337
14 2024-08-05 3.8600 -0.0445545 427.89 -10943624.0000 -0.21893
15 2024-08-02 4.0400 -0.0334928 472.996 -14696772.0000 -0.257066
16 2024-08-01 4.1800 0.00966184 497.435 7710109.8000 0.126305
17 2024-07-31 4.1400 0.0298507 690.852 9603634.0000 0.115494
18 2024-07-30 4.0200 0.0151515 816.721 2341341.9200 0.0239997
19 2024-07-29 3.9600 0.0179949 373.239 5214741.0000 0.12152
r0_net r0_ratio r0x_ratio cnt_r0x_ratio cate_ra \
0 -12144784.0000 -0.03841832 -52.3118 -1 0.0214639
1 4221340.0000 0.09142500 42.2921 1 -0.0486805
...
16 -1288309434.8920
17 13484819579.9720
18 4050788991.6900
19 796606779.8250
重新命名列名,让数据可以看懂:
# 重新命名列名,并返回一个新的DataFrame(inplace=False)
new_columns={'opendate': '交易日',
'trade': '收盘价',
'changeratio': '涨跌幅',
'turnover':'换手率',
'netamount': '净流入(元)',
'ratioamount': '净流入率',
'r0_net': '主力净流入(元)',
'r0_ratio':'主力净流入率',
'r0x_ratio': '主力罗盘(°)',
'cate_ra': '行业净流入率'}
# 返回新的DataFrame
result = df.rename(columns=new_columns, inplace=False)
print(result)
result.to_excel('bad_demo.xlsx', index=False)
标签:调用,ratio,读取,08,接口,2024,bad,code,page
From: https://blog.csdn.net/Humbunklung/article/details/141505962