python爬取IHO,上证50,以及两个上证ETF基金并进行对比
程序员文章站
2022-03-27 21:05:50
IHO在新浪财经爬取上证50在东方财富网爬取另外两个上证ETF基金编号分别为510100, 510050在同花顺上爬取这些网站的图表数据都是通过json传递的所以找到对应的api即可抓取数据以下是单纯分析上证50的源代码显示涨幅的折线图# -*- coding: utf-8 -*-"""Created on Sun Nov 29 21:31:07 2020@author: asus"""import pandas as pdimport numpy as npimport m...
IHO在新浪财经爬取
上证50在东方财富网爬取
另外两个上证ETF基金编号分别为510100, 510050在同花顺上爬取
这些网站的图表数据都是通过json传递的
所以找到对应的api即可抓取数据
以下是单纯分析上证50的源代码
显示涨幅的折线图以及存储涨幅数据的EXCEL
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 29 21:31:07 2020
@author: asus
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import urllib.request as urllib2
import json
import xlwt
def savedata(name,data):
#存数据
xls=xlwt.Workbook()
sht1=xls.add_sheet(name)
#添加字段
sht1.write(0,0,'日期')
sht1.write(0,1,'涨幅')
i=0
for item in data:
sht1.write(i+1,0,item[0])
sht1.write(i+1,1,item[1])
i=i+1
xls.save('./'+name+'RateData.xls')
#上证50
shangzheng50Url = 'http://push2his.eastmoney.com/api/qt/stock/kline/get?cb=jQuery1124034703156772714716_1606741623783&secid=1.000016&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58&klt=101&fqt=0&beg=19900101&end=20220101&_=1606741623987'
result = []
shangzhengReq=urllib2.Request(shangzheng50Url)
shangzhengRespon=urllib2.urlopen(shangzhengReq)
shangzhengRes = shangzhengRespon.read().decode()
shangzhengRes=shangzhengRes[shangzhengRes.find('klines')+8:len(shangzhengRes)-4]
shangzhengRes_json = json.loads(shangzhengRes)
result2=[]
lastvalue=shangzhengRes_json[0].split(',')[2]
for item in shangzhengRes_json:
items=item.split(',')
todayvalue=items[2]
todayrate=0
if(float(todayvalue)-float(lastvalue)!=0):
todayrate=(float(todayvalue)-float(lastvalue))/float(lastvalue)
lastvalue=todayvalue
#日期加收盘指数
result2.append([items[0],todayrate])
savedata('shangzheng50',result2[-101:-1])
result.append(result2[-101:-1])
close_result = []
for instrument in result:
oneDay_list = []
for oneDay in instrument:
oneDay_list.append(float(oneDay[-1]))
close_result.append(np.array(oneDay_list))
close_result = np.array(close_result)
close_result = close_result.T
df = pd.DataFrame(data=close_result, columns=['shangzheng50'])
df.plot()
plt.show()
运行该程序可以得到下图
同理将另外三个读取分析一遍就可以得到
涨幅对比折线图
同时换个方法解析的正相关图
如果有帮助到你,能点个赞吗!
本文地址:https://blog.csdn.net/qq_40044912/article/details/110455651