1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
import os, sys, time import urllib import urllib2 import xlwt
update_data = [] down_data = []
t = time.time() now_time = int(round(t * 1000)) now_time = str(now_time)
codes = ['6008591','6002361','0004002','0022012','6002971','0025792','6000281','0000012','3001662'] print '##################################################################'
xls = xlwt.Workbook() sheet = xls.add_sheet("Sheet1") row = 0
sheet.write(row, 0, 'time') r = 1 for i in codes: sheet.write(row, r, i) r += 1 sheet.write(row, r, 'price'+i) r += 1
file_name = 'piao'+now_time + '.xls'
while True: time.sleep(5) use = [] t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) use.append(t) column = 0 for code in codes: url = 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + code + '&sty=DPTTFD&st=z&sr=&p=&ps=&cb=&js=var%20fenshiEntity=(x)&token=7bc05d0d4c3c22ef9fca8c2a912d779c&_=' + now_time req = urllib2.Request(url) try: response = urllib2.urlopen(req) except: print 'connect error try agian...' try: response = urllib2.urlopen(req) except: print 'connect error try agian and agian...' response = urllib2.urlopen(req) responsedata = response.read() if responsedata == 'var fenshiEntity="-"' : print 'check the url:'+url print 'have no data begian....' print 'wait 1 mins....' time.sleep(60) continue
new_data = responsedata.split('|') for data in new_data: data_item = data.split('~') if data_item[3] == '1': update_data.append(data_item) else: down_data.append(data_item)
update_data_priece = [] for update_data_item in update_data: priece = float(update_data_item[1]) * int(update_data_item[2]) priece = float('%.2f' % priece) update_data_priece.append(priece)
down_data_priece = [] for down_data_item in down_data: priece = float(down_data_item[1]) * int(down_data_item[2]) priece = float('%.2f' % priece) down_data_priece.append(priece)
bi = sum(update_data_priece) / sum(down_data_priece) * 100 bi = float('%.2f' % bi) use.append(bi)
pan_url = 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd='+ code +'&sty=MPNASDP&st=a&sr=1&p=1&ps=1000&cb=&js=var%20pankou=(x)&token=7bc05d0d4c3c22ef9fca8c2a912d779c&_=' + now_time pan_req = urllib2.Request(pan_url) try: pan_response = urllib2.urlopen(pan_req) except: print 'connect error try agian...' try: pan_response = urllib2.urlopen(pan_req) except: print 'connect error try agian and agian...' pan_response = urllib2.urlopen(pan_req) pan_responsedata = pan_response.read() new_pan_data = pan_responsedata.split(',') pan_p = float(new_pan_data[3]) use.append(pan_p)
print use row += 1 for i in range(len(use)): sheet.write(row, i, use[i]) xls.save(file_name)
|