python爬虫(正则取数据)读取表格内的基金代码后爬取基金最新净值,同时写到对应的表格中,基于最近一次购买净值计算出涨跌幅(名字有点长)
最近基金跌的真够猛,虽说是定投,但大幅度下跌,有时候适当的增加定投数也是降低平均成本的一种方式
每天去看去算太费时间,写了个爬虫,让他自动抓数据后自动计算出来吧
实现逻辑:
1、创建了一个excel表格,把当前定投的基金都备注到里面、
2、脚本依次读取表格中的基金代码
3、拿到基金代码,到“天天基金网”获取基金最新净值
4、把获取到净值和更新时间写到excel中
5、excel公示计算出基于最近一次购买值的涨跌幅
后续还可以把脚本搬到云上,每天自动运行,达到设置的跌幅发邮件提醒或者短信提醒,这都是后话
这里只写代码部分
第一步,读取表格中的基金代码,脚本如下
#读取表格内的基金代码 def code(): wb = xlrd.open_workbook(path+'\\更新数据.xlsx')# 打开Excel文件 data = wb.sheet_by_name('Sheet1')#通过excel表格名称(rank)获取工作表 b=data.col_values(1)#获取第一列数据(数组) list=[] for c in b[1:]:#for循环,排除第一行数据 d=int(c) s="%06d" % d#股票代码一共有6位,常规打印无法打印出首位带0的代码的0部分,补齐缺失的0 #print(s) list.append(s) return(list) code=code()
返回数据如下格式
“['161005', '161903', '110003', '160222', '000248', '163406']”
这一步涉及到对excel操作(读取)处理,请参考:https://www.cnblogs.com/becks/p/11397995.html
第二步,从上一步返回的基金代码list中取值,拼接成有效链接爬取基金信息,脚本如下
def data(cookies,headers,params,code): valuelist = []
timelist = [] for num in code: response = requests.get('http://fund.eastmoney.com/'+num+'.html', headers=headers, params=params, cookies=cookies, verify=False) response.encoding = 'utf-8' #处理编码得步骤 response = response.text #获取当前基金净值 valueD = re.findall('<span class="fix_dwjz bold ui-color-green">(.+?)</span>', response, re.S) if len(valueD)>0: #如果返回值长度大于0,即可理解为当天是跌,value从valueD取值 value = valueD else: #否则,即可理解为当天是涨,value从valueZ取值 valueZ = re.findall('<span class="fix_dwjz bold ui-color-red">(.+?)</span>', response, re.S)#当天净值为涨 value = valueZ #print(value) #获取当前最新净值更新时间 valuetime = re.findall('<div class="titleItems tabBtn titleItemActive" data-date="(.+?)" data-href-more', response, re.S) valuelist.append(value[0]) timelist.append(valuetime) return(valuelist,timelist) test=data(cookies,headers,params,code)
返回数据如下:这一步没有对数据进行处理,里面包含了所有基金净值和更新时间
“(['3.7090', '2.1096', '2.5932', '1.2488', '3.2830', '2.1112'], ['2021-03-05'])”
这一步涉及对数据的正则处理,可参考:https://www.cnblogs.com/becks/p/14494929.html
第三步,处理数据,并保存到excel中,脚本如下
def save(test): xfile = openpyxl.load_workbook(path+'\\更新数据.xlsx') sheet1 = xfile.worksheets[0]for i in range(len(test[0])): sheet1.cell(i+2, 5).value=test[0][i] for i in range(len(test[0])): sheet1.cell(i+2, 6).value=test[1][i][0] xfile.save(path+'\\更新数据.xlsx') save(test)
这一步涉及到对excel进行操作(追加写入),可参考:https://www.cnblogs.com/becks/p/12250052.html
整个脚本执行后,将自动更新下图表格中的红框数据,且涨跌幅一栏也会基于购买时的净值,计算得出结果
需要注意的是,excel文件的命名还有存放位置需要跟脚本在一个目录下
附完整代码
# -*-coding:utf8-*- # encoding:utf-8 import requests import os import sys import re import time import random import openpyxl import xlrd path = os.path.abspath(os.path.dirname(sys.argv[0])) cookies = { 'intellpositionL': '1010.67px', 'em_hq_fls': 'js', 'em-quote-version': 'topspeed', 'qgqp_b_id': '134fc3fcff10a2a2c4035eebfe40f119', 'intellpositionT': '455px', 'HAList': 'a-sh-600909-%u534E%u5B89%u8BC1%u5238%2Ca-sh-605003-%u4F17%u671B%u5E03%u827A%2Ca-sz-300855-%u56FE%u5357%u80A1%u4EFD%2Ca-sz-300015-%u7231%u5C14%u773C%u79D1%2Ca-sh-603939-%u76CA%u4E30%u836F%u623F%2Ca-sz-300677-%u82F1%u79D1%u533B%u7597%2Ca-sh-600036-%u62DB%u5546%u94F6%u884C%2Ca-sz-000860-%u987A%u946B%u519C%u4E1A%2Ca-sz-002352-%u987A%u4E30%u63A7%u80A1%2Ca-sz-002034-%u65FA%u80FD%u73AF%u5883', 'kforders': '0%3B-1%3B%3B%3B0%2C2%2C24%2C25%2C18%2C19%2C22%2C23%2C21%2C3', 'Eastmoney_Fund_Transform': 'true', 'Eastmoney_Fund': '000297_000001_000011', 'st_si': '89636864382855', 'st_asi': 'delete', 'ASP.NET_SessionId': 'kbxmonijodtnjbezidg0jzmq', 'searchbar_code': '519736_000032_000205_040040_206018_000248_110003_163406_161903_161005', 'EMFUND0': '02-08%2015%3A15%3A53@%23%24%u6613%u65B9%u8FBE%u4FE1%u7528%u503A%u503A%u5238A@%23%24000032', 'EMFUND2': '02-08%2015%3A16%3A12@%23%24%u534E%u5B89%u7EAF%u503A%u503A%u5238A@%23%24040040', 'EMFUND1': '02-08%2015%3A16%3A07@%23%24%u6613%u65B9%u8FBE%u6295%u8D44%u7EA7%u4FE1%u7528%u503A%u503A%u5238A@%23%24000205', 'EMFUND3': '02-08%2015%3A26%3A18@%23%24%u9E4F%u534E%u4EA7%u4E1A%u503A%u503A%u5238@%23%24206018', 'EMFUND4': '02-08%2015%3A27%3A25@%23%24%u6C47%u6DFB%u5BCC%u4E2D%u8BC1%u4E3B%u8981%u6D88%u8D39ETF%u8054%u63A5@%23%24000248', 'EMFUND5': '02-08%2015%3A28%3A40@%23%24%u6613%u65B9%u8FBE%u4E0A%u8BC150%u589E%u5F3AA@%23%24110003', 'EMFUND6': '02-08%2015%3A29%3A32@%23%24%u5174%u5168%u5408%u6DA6%u6DF7%u5408%28LOF%29@%23%24163406', 'EMFUND7': '02-08%2015%3A30%3A22@%23%24%u4E07%u5BB6%u884C%u4E1A%u4F18%u9009%u6DF7%u5408%28LOF%29@%23%24161903', 'EMFUND8': '03-07%2020%3A24%3A02@%23%24%u5357%u65B9%u660C%u5143%u8F6C%u503AA@%23%24006030', 'st_pvi': '18238554782275', 'st_sp': '2020-01-23%2014%3A56%3A33', 'st_inirUrl': 'https%3A%2F%2Fwww.baidu.com%2Flink', 'st_sn': '5', 'st_psi': '20210307202806812-112200305282-6714006413', } headers = { 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'If-None-Match': 'W/"6044c497-25fbe"', 'If-Modified-Since': 'Sun, 07 Mar 2021 12:18:31 GMT', } params = ( ('spm', 'search'), ) #读取表格内的基金代码 def code(): wb = xlrd.open_workbook(path+'\\更新数据.xlsx')# 打开Excel文件 data = wb.sheet_by_name('Sheet1')#通过excel表格名称(rank)获取工作表 b=data.col_values(1)#获取第一列数据(数组) list=[] for c in b[1:]:#for循环,排除第一行数据 d=int(c) s="%06d" % d#股票代码一共有6位,常规打印无法打印出首位带0的代码的0部分,补齐缺失的0 #print(s) list.append(s) return(list) code=code() def data(cookies,headers,params,code): valuelist = [] timelist = [] for num in code: response = requests.get('http://fund.eastmoney.com/'+num+'.html', headers=headers, params=params, cookies=cookies, verify=False) response.encoding = 'utf-8' #处理编码得步骤 response = response.text #获取当前基金净值 valueD = re.findall('<span class="fix_dwjz bold ui-color-green">(.+?)</span>', response, re.S) if len(valueD)>0: #如果返回值长度大于0,即可理解为当天是跌,value从valueD取值 value = valueD else: #否则,即可理解为当天是涨,value从valueZ取值 valueZ = re.findall('<span class="fix_dwjz bold ui-color-red">(.+?)</span>', response, re.S)#当天净值为涨 value = valueZ #print(value) #获取当前最新净值更新时间 valuetime = re.findall('<div class="titleItems tabBtn titleItemActive" data-date="(.+?)" data-href-more', response, re.S) valuelist.append(value[0]) timelist.append(valuetime) return(valuelist,timelist) test=data(cookies,headers,params,code) def save(test): xfile = openpyxl.load_workbook(path+'\\更新数据.xlsx') sheet1 = xfile.worksheets[0] for i in range(len(test[0])): sheet1.cell(i+2, 5).value=test[0][i] for i in range(len(test[0])): sheet1.cell(i+2, 6).value=test[1][i][0] xfile.save(path+'\\更新数据.xlsx') save(test) def over(test): print("数据已更新",test[1][0]) over(test)