|
| 1 | +import re |
| 2 | +import os |
| 3 | +import requests |
| 4 | +import json |
| 5 | +import pandas as pd |
| 6 | + |
| 7 | +pd.set_option('display.max_columns', None) |
| 8 | +pd.set_option('display.max_rows', None) |
| 9 | +pd.set_option('display.unicode.ambiguous_as_wide', True) |
| 10 | +pd.set_option('display.unicode.east_asian_width', True) |
| 11 | +pd.set_option('display.width', 5000) |
| 12 | +headers = { |
| 13 | + "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"} |
| 14 | +# 功能:解析可还债网页 |
| 15 | +def HTML(url,time_,time_str): |
| 16 | + gupiao_list = [] |
| 17 | + try: |
| 18 | + # 解析网页 |
| 19 | + r = requests.get(url, headers=headers, timeout=30) |
| 20 | + r.raise_for_status() |
| 21 | + r.encoding = r.apparent_encoding |
| 22 | + html = r.text |
| 23 | + except Exception as e: |
| 24 | + print("wrong:" + e) |
| 25 | + pass |
| 26 | + pat = re.compile("\[\{.*?\}\]") |
| 27 | + data = pat.findall(html) |
| 28 | + # 转换为json格式 |
| 29 | + js = json.loads(data[0]) |
| 30 | + # 循环写入数据 |
| 31 | + for i in range(len(js)): |
| 32 | + # print(js[i]) |
| 33 | + if js[i]["LISTDATE"] != "-": |
| 34 | + print(str(js[i]["BONDCODE"])) |
| 35 | + time.sleep(1) |
| 36 | + lilv,jinkia,zhenfu,zuigao,zuidi,zuoshou,chengjiaoliang,chengjiaoe =get_data(str(js[i]["BONDCODE"]),time_) |
| 37 | + print( lilv, jinkia, zhenfu, zuigao, zuidi, zuoshou, chengjiaoliang, chengjiaoe) |
| 38 | + list = [lilv, jinkia, zuigao,zuidi,zuoshou] |
| 39 | + if lilv>1000: |
| 40 | + lilv = lilv /10 |
| 41 | + if jinkia > 1000: |
| 42 | + jinkia = jinkia / 10 |
| 43 | + if zuigao > 1000: |
| 44 | + zuigao = zuigao / 10 |
| 45 | + if zuidi > 1000: |
| 46 | + zuidi = zuidi / 10 |
| 47 | + if zuoshou > 1000: |
| 48 | + zuoshou = zuoshou / 10 |
| 49 | + |
| 50 | + # time.sleep(1) |
| 51 | + data=(js[i]["SNAME"], js[i]["BONDCODE"], js[i]["CORRESCODE"], js[i]["STARTDATE"],lilv,jinkia,zhenfu,zuigao,zuidi,zuoshou,chengjiaoliang,chengjiaoe) |
| 52 | + gupiao_list.append(data) |
| 53 | + title = ["债券简称","债券代码", "正股代码","上市时间","现价","今开","振幅","最高","最低","昨收","成交量","成交额"] |
| 54 | + df = pd.DataFrame(gupiao_list, columns=title) |
| 55 | + to_csv(df, f"id{time_str}.csv") |
| 56 | + |
| 57 | +# 保存csv格式的文件夹 |
| 58 | +def to_csv(df, csv_file): |
| 59 | + if os.path.exists(csv_file) == False: |
| 60 | + df.to_csv(csv_file, index=False) |
| 61 | + else: |
| 62 | + df.to_csv(csv_file, mode='a+', header=False, index=False) |
| 63 | + |
| 64 | + |
| 65 | +# 获取每个可还债的数据 |
| 66 | +# 输入:可还债id,与时间 |
| 67 | +# 输出:"现价","今开","振幅","最高","最低","昨收","成交量","成交额" 的数据 |
| 68 | +def get_data(id,time_): |
| 69 | + url ="http://push2.eastmoney.com/api/qt/stock/get?secid=1."+str(id)+"&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f43,f169,f170,f46,f60,f84,f116,f44,f45,f171,f126,f47,f48,f168,f164,f49,f161,f55,f92,f59,f152,f167,f50,f86,f71,f172,f182,f191,f192,f532&cb=jQuery1124021434030444820706_"+str(time_)+"000"+"&type=CT&cmd=1280922&sty=FDPBPFB&st=z&js=((x))&token=4f1862fc3b5e77c150a2b985b12db0fd&_="+str(time_)+"000" |
| 70 | + # url = "http://push2.eastmoney.com/api/qt/stock/get?secid=1."+str(id)+"&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f43,f169,f170,f46,f60,f84,f116,f44,f45,f171,f126,f47,f48,f168,f164,f49,f161,f55,f92,f59,f152,f167,f50,f86,f71,f172,f182,f191,f192,f532&cb=jQuery1124002590768518466602_1588042202265&type=CT&cmd=1230442&sty=FDPBPFB&st=z&js=((x))&token=4f1862fc3b5e77c150a2b985b12db0fd&_=1588042202266" |
| 71 | + print(url) |
| 72 | + try: |
| 73 | + r = requests.get(url, headers=headers, timeout=30) |
| 74 | + r.raise_for_status() |
| 75 | + r.encoding = r.apparent_encoding |
| 76 | + html = r.text |
| 77 | + except Exception as e: |
| 78 | + print("wrong:" + e) |
| 79 | + print(html) |
| 80 | + try: |
| 81 | + try: |
| 82 | + # 获取内容 |
| 83 | + list_ = [] |
| 84 | + pat = re.compile("({.*?\})") |
| 85 | + data = pat.findall(html+"}}") |
| 86 | + d =eval(data[0]+"}") |
| 87 | + new_data = d.get("data") |
| 88 | + # print(new_data) |
| 89 | + lilv= new_data.get("f43") # 利率 |
| 90 | + jinkia = new_data.get("f46") # 今开 |
| 91 | + zhenfu=new_data.get("f171") # 振幅 |
| 92 | + zuigao = new_data.get("f44") # 最高 |
| 93 | + zuidi = new_data.get("f45") # 最低 |
| 94 | + zuoshou = new_data.get("f60") # 昨收 |
| 95 | + chengjiaoliang = new_data.get("f47") |
| 96 | + chengjiaoe = new_data.get("f48") |
| 97 | + return round(float(lilv)/100,2), round(float(jinkia)/100,2), str(round(float(zhenfu),2)) +"%", round(float(zuigao)/100,2), round(float(zuidi)/100,2), round(float(zuoshou)/100,2), chengjiaoliang, chengjiaoe |
| 98 | + except: |
| 99 | + print("*" * 100) |
| 100 | + # url = "http://push2.eastmoney.com/api/qt/stock/get?secid=0." + str( |
| 101 | + # id) + "&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f43,f169,f170,f46,f60,f84,f116,f44,f45,f171,f126,f47,f48,f168,f164,f49,f161,f55,f92,f59,f152,f167,f50,f86,f71,f172,f182,f191,f192,f532&cb=jQuery1124021434030444820706_" + str( |
| 102 | + # time_) + "000" + "&type=CT&cmd=1280922&sty=FDPBPFB&st=z&js=((x))&token=4f1862fc3b5e77c150a2b985b12db0fd&_=" + str( |
| 103 | + # time_) + "000" |
| 104 | + url = "http://push2.eastmoney.com/api/qt/stock/get?secid=1." + str( |
| 105 | + id) + "&ut=bd1d9ddb04089700cf9c27f6f7426281&fields=f43,f169,f170,f46,f60,f84,f116,f44,f45,f171,f126,f47,f48,f168,f164,f49,f161,f55,f92,f59,f152,f167,f50,f86,f71,f172,f182,f191,f192,f532&cb=jQuery1124021434030444820706_" + str( |
| 106 | + time_) + "000" + "&type=CT&cmd=1280922&sty=FDPBPFB&st=z&js=((x))&token=4f1862fc3b5e77c150a2b985b12db0fd&_=" + str( |
| 107 | + time_) + "000" |
| 108 | + try: |
| 109 | + r = requests.get(url, headers=headers, timeout=30) |
| 110 | + r.raise_for_status() |
| 111 | + r.encoding = r.apparent_encoding |
| 112 | + html = r.text |
| 113 | + except Exception as e: |
| 114 | + print("wrong:" + e) |
| 115 | + # print(html) |
| 116 | + pat = re.compile("({.*?\})") |
| 117 | + data = pat.findall(html + "}}") |
| 118 | + d = eval(data[0] + "}") |
| 119 | + new_data = d.get("data") |
| 120 | + lilv = new_data.get("f43") # 利率 |
| 121 | + jinkia = new_data.get("f46") # 今开 |
| 122 | + zhenfu = new_data.get("f171") # 振幅 |
| 123 | + zuigao = new_data.get("f44") # 最高 |
| 124 | + zuidi = new_data.get("f45") # 最低 |
| 125 | + zuoshou = new_data.get("f60") # 昨收 |
| 126 | + chengjiaoliang = new_data.get("f47") |
| 127 | + chengjiaoe = new_data.get("f48") |
| 128 | + # print( lilv, jinkia, zhenfu, zuigao, zuidi, zuoshou, chengjiaoliang, chengjiaoe) |
| 129 | + return round(float(lilv)/100,2), round(float(jinkia)/100,2), str(round(float(zhenfu),2)) +"%", round(float(zuigao)/100,2), round(float(zuidi)/100,2), round(float(zuoshou)/100,2), chengjiaoliang, chengjiaoe |
| 130 | + except: |
| 131 | + return 0,0,0,0,0,0,0,0 |
| 132 | + |
| 133 | +import time |
| 134 | +# 获取可还债网页的所有内容 |
| 135 | +# 输入:时间类型 |
| 136 | +# 输出: csv文件 |
| 137 | +def main(time_,time_str): |
| 138 | + for i in range(1, 9): |
| 139 | + url = "http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?type=KZZ_LB2.0&token=70f12f2f4f091e459a279469fe49eca5&cmd=&st=STARTDATE&sr=-1&p=" + str( |
| 140 | + i) + "&ps=50&js=var%20GCPJwVrm={pages:(tp),data:(x),font:(font)}&rt=52919237" |
| 141 | + print(url) |
| 142 | + HTML(url,time_,time_str) |
| 143 | + time.sleep(2) |
| 144 | + |
| 145 | +# 字符类型的时间: |
| 146 | +def get_time(time_str): |
| 147 | + # 转为时间数组 |
| 148 | + timeArray = time.strptime(time_str, "%Y%m%d") |
| 149 | + # 转为时间戳 |
| 150 | + timeStamp = int(time.mktime(timeArray)) |
| 151 | + return timeStamp |
| 152 | + |
| 153 | +if __name__ == '__main__': |
| 154 | + # 获取当前时间 |
| 155 | + for time_str in ["20200426"]: |
| 156 | + time_ = get_time(time_str) |
| 157 | + print("时间戳",time_) |
| 158 | + # 主程序入口 |
| 159 | + main(time_,time_str) |
0 commit comments