python制作一个基金数据下载器
↑↑↑关注后"星标"简说Python人人都可以简单入门Python、爬虫、数据分析简说Python推荐来源:Python数据分析之禅作者:小dull鸟Oneoldw...
↑↑↑关注后"星标"简说Python
人人都可以简单入门Python、爬虫、数据分析
简说Python推荐
来源:Python数据分析之禅 作者:小dull鸟
One old watch, like brief python
受公众号读者之邀,做了一个电脑桌面版基金数据下载器,可以根据基金代码、日期等信息下载相应数据,成品如下:
下面把制作方法详细介绍给大家
1.抓包获取基金数据接口
通过抓包天天基金单个基金数据,研究得出数据接口为:
http://api.fund.eastmoney.com/f10/lsjz?fundCode=基金代码&pageIndex=1&pageSize=10000&startDate=开始日期&endDate=截止日期&_=1555586870418?
fundCode为基金代码
pageIndex为页码,这里固定为1
pageSize为返回数据条数
startDate为开始日期
enddate为截止日期
返回的为json格式数据:
2.写爬虫代码
这个爬虫代码代码比较简单,需要注意一点,要在header头中把Referer信息加上,否则会被反爬,这里直接把代码贴出来:
fundCode = '519670' #基金代码
pageIndex = 1
startDate = '2020-10-18' #起始时间
endDate = '2020-10-21' #截止时间
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
'Referer': 'http://fundf10.eastmoney.com/jjjz_{0}.html'.format(fundCode)
}
url = 'http://api.fund.eastmoney.com/f10/lsjz?fundCode={0}&pageIndex={1}&pageSize=5000&startDate={2}&endDate={3}&_=1555586870418?'.format(fundCode, pageIndex, startDate, endDate)
response = requests.get(url, headers=header)
results=json.loads(response.text)['Data']['LSJZList']
for i in results:
print(i)
FSRQ=i['FSRQ'] #日期
DWJZ = i['DWJZ'] #单位净值
LJJZ = i['LJJZ'] #累计净值
JZZZL = i['JZZZL'] #日增长率
SGZT = i['SGZT'] #申购状态
SHZT = i['SHZT'] #赎回状态
result=[FSRQ,DWJZ,LJJZ,JZZZL,SGZT,SHZT]
with open('1.csv', 'a+', newline='', encoding='gb18030') as f:
f_csv = csv.writer(f)
f_csv.writerow(result)
3.用designer搭建可视化界面
在pycharm中按如下方式打开Qtdesigner:
通过左侧栏的工具拖曳形成如下界面:
并赋给“开始下载”按钮以‘pushButton_click’函数,赋给“保存路径”按钮以‘setBrowerPath’函数
4.将ui文件转为py代码
选中生成的ui文件,右键按如下方式转换成py文件:
5.将爬虫代码融入py代码中
首先充实setBrowerPath函数,获取下载路径,把路径在输出框显示出来:
def setBrowerPath(self):
self.download_path = QFileDialog.getExistingDirectory(self)
self.textEdit_6.setText(self.download_path)
然后在pushButton_click函数中添加代码, 接收界面输入的基金代码、起始时间以及截至时间参数,导入接口url中,代码如下:
def pushButton_click(self):
self.textEdit_4.setText("下载中......")
self.fundCode = self.codeEdit.text() # 基金代码
self.startDate = self.dateTime_start.text() # 起始时间
self.endDate = self.dateTime_end.text() # 截止时间
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0',
'Referer': 'http://fundf10.eastmoney.com/jjjz_{0}.html'.format(self.fundCode)
}
url = 'http://api.fund.eastmoney.com/f10/lsjz?fundCode={0}&pageIndex={1}&pageSize=10000&startDate={2}&endDate={3}&_=1555586870418?'.format(
self.fundCode, 1, self.startDate, self.endDate)
response = requests.get(url, headers=header)
results = json.loads(response.text)['Data']['LSJZList']
for i in results:
FSRQ = i['FSRQ'] # 日期
DWJZ = i['DWJZ'] # 单位净值
LJJZ = i['LJJZ'] # 累计净值
JZZZL = i['JZZZL'] # 日增长率
SGZT = i['SGZT'] # 申购状态
SHZT = i['SHZT'] # 赎回状态
result = [FSRQ, DWJZ, LJJZ, JZZZL, SGZT, SHZT]
with open('{0}/{1}基金数据{2}-{3}.csv'.format(self.download_path,self.fundCode,self.startDate,self.endDate), 'a+', newline='', encoding='gb18030') as f:
f_csv = csv.writer(f)
f_csv.writerow(result)
self.textEdit_4.append("下载成功")
self.textEdit_4.append("表格保存路径:{}".format(self.download_path))
self.textEdit_4.append("表格名字:{}".format('{0}基金数据{1}-{2}.csv'.format(self.fundCode,self.startDate,self.endDate)))
最后给界面添加背景:
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap(r"D:\数据分析\38.python做一个基金数据下载器\背景.jpg")))
self.setPalette(window_pale)
6.用pyinstaller命令打包成.exe文件
在amd控制台使用CD进入py文件所在目录:
cd C:\Users\zhengchangliang\Desktop\PythonProject\mypyqt
生成exe文件:
pyinstaller -F -w fund.py
在该文件夹下,会发现生成1个dist文件夹,打开后,里面有1个fund.exe文件,在创建快捷方式并发送至桌面,以后就可以直接使用了
文中完整代码已经打包整理完毕,请扫码添加我的微信,回复基金数据获取,
代码遇有问题,也可以微信沟通交流
11.1就截止啦,点击参与
简说Python
长按扫码关注,一起学Python
学习更多:
整理了我开始分享学习笔记到现在超过250篇优质文章,涵盖数据分析、爬虫、机器学习等方面,别再说不知道该从哪开始,实战哪里找了
更多推荐
所有评论(0)