作者:明天爱你1314_138 | 来源:互联网 | 2024-10-30 17:40
本项目旨在优化可转债数据的智能抓取与分析平台。通过爬取集思录上的可转债信息(排除已发布赎回的债券),并结合安道全教授提出的三条安全线投资策略,新增了建仓线、加仓线和重仓线,以提供更精准的投资建议。
本代码是爬取集思录可转债信息,(除去了发布赎回的,根据安道全老师的三条安全线投资法添加了建仓线、加仓线、重仓线),价格达到三条线的,给与操作提示。
import json
import requests
import csv
import re
from lxml import etreedef get_dat():headers &#61; {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",}newUrl &#61;"https://www.jisilu.cn/data/cbnew/cb_list/?___jsl&#61;LST___t&#61;1584777951900"response &#61; requests.get(newUrl)data &#61; response.content.decode("utf-8")dat &#61; json.loads(data)lst_data &#61; []for one in dat[&#39;rows&#39;]:lst_dat &#61; []id &#61; one["id"]dat_cell &#61; one["cell"]is_shui &#61; dat_cell[&#39;force_redeem&#39;]if is_shui &#61;&#61; None:name &#61; dat_cell[&#39;bond_nm&#39;]price &#61; dat_cell[&#39;price&#39;]premium_rt &#61; dat_cell[&#39;premium_rt&#39;]rating_cd &#61; dat_cell[&#39;rating_cd&#39;]put_convert_price &#61; dat_cell[&#39;put_convert_price&#39;]force_redeem_price &#61; dat_cell[&#39;force_redeem_price&#39;]last_time &#61; dat_cell[&#39;year_left&#39;]dblow &#61; dat_cell[&#39;dblow&#39;]xiangqing_url &#61; &#39;https://www.jisilu.cn/data/convert_bond_detail/&#39; &#43; idxiangqing_response &#61; requests.get(xiangqing_url)html &#61; xiangqing_response.content.decode("utf-8")html &#61; etree.HTML(html)lixi &#61; html.xpath(&#39;.//td[&#64;id&#61;"cpn_desc"]/text()&#39;)pattern &#61; re.compile(r&#39;\d&#43;\.\d&#43;?&#39;) lixi &#61; pattern.findall(lixi[0])shuhuijia &#61; html.xpath(&#39;.//td[&#64;id&#61;"redeem_price"]/text()&#39;)li_price &#61; 0for li in lixi:li_price &#61; li_price &#43; float(li)try:jiancang &#61; float(shuhuijia[0]) &#43; (li_price - float(lixi[-1])) * 0.8except:jiancang &#61; 0if jiancang !&#61; 0 and float(price) - jiancang < 3:is_oper &#61; &#39;建仓&#39;if jiancang !&#61; 0 and float(price) - float(shuhuijia[0]) < 3:is_oper &#61; &#39;加仓&#39;else:is_oper &#61; &#39;&#39;lst_dat.append(id)lst_dat.append(name)lst_dat.append(price)lst_dat.append(jiancang)lst_dat.append(shuhuijia[0])lst_dat.append(&#39;&#39;)lst_dat.append(premium_rt)lst_dat.append(rating_cd)lst_dat.append(put_convert_price)lst_dat.append(force_redeem_price)lst_dat.append(last_time)lst_dat.append(dblow)lst_dat.append(is_oper)lst_data.append(lst_dat)else:continuereturn lst_datadef wirte_csv(data):f &#61; open(&#39;可转债.csv&#39;, &#39;w&#39;, encoding&#61;&#39;utf-8&#39;, newline&#61;&#39;&#39;)csv_writer &#61; csv.writer(f)csv_writer.writerow(["代 码", "转债名称", "现 价", "建仓线", "加仓线", "重仓线", "溢价率", "评级","回售触发价", "强赎触发价", "剩余年限", "双低", "操作"])for dat in data:csv_writer.writerow(dat)f.close()if __name__ &#61;&#61; &#39;__main__&#39;:data &#61; get_dat()wirte_csv(data)
结果&#xff1a;