python抓取股票信息_Python股票信息抓取(三)

1 #-*-coding:utf-8 -*-

2 importurllib3 importre4 importjson5 importurllib26 from lxml importetree7 importrequests8 importtime9 from Queue importQueue10 from pymongo importMongoClient11 importmatplotlib.pyplot as plt12 URL = 'http://quote.fx678.com/exchange/WH'

13 nation_que =Queue()14 client = MongoClient('localhost',27017)15 db =client.test16 Share =db.Share17

18 defsub_sort(array,array1,low,high):19 key =array[low]20 key1 =array1[low]21 while low <22 while low high and array>=key:23 high -= 122>

24 while low < high and array[high] <25 array array1 low>

28 array[high] =array[low]29 array1[high] =array1[low]30 array[low] =key31 array1[low] =key132 returnlow33

34

35 defquick_sort(array,array1,low,high):36 if low <37 key_index="sub_sort(array,array1,low,high)38" quick_sort>

41 def download(url, headers, num_try=2):42 while num_try >0:43 num_try -= 1

44 try:45 content = requests.get(url, headers=headers)46 returncontent.text47

48 excepturllib2.URLError as e:49 print 'Download error', e.reason50

51 returnNone52

53 current_quto =Queue()54 open_quto =Queue()55 high_quto =Queue()56 low_quto =Queue()57 close_quto =Queue()58 update_time =Queue()59 defget_type_url():60 headers ={61 'User_agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',62 'Referer': 'http://quote.fx678.com/exchange/WH',63 'Cookie': 'io=-voMclEjiizK9nWKALqB; UM_distinctid=15f5938ddc72db-089cf9ba58d9e5-31657c00-fa000-15f5938ddc8b24; Hm_lvt_d25bd1db5bca2537d34deae7edca67d3=1509030420; Hm_lpvt_d25bd1db5bca2537d34deae7edca67d3=1509031023',64 'Accept-Language': 'zh-CN,zh;q=0.8',65 'Accept-Encoding': 'gzip, deflate',66 'Accept': '*/*'

67 }68 content =download(URL,headers)69 html =etree.HTML(content)70 result = html.xpath('//a[@class="mar_name"]/@href')71 result1 = html.xpath('//td/text()')72 num =073 for each inresult1:74

75 if num%6 ==0:76 current_quto.put(each)77 num += 1

78 elif num%6 == 1:79 open_quto.put(each)80 num += 1

81 elif num%6 == 2:82 high_quto.put(each)83 num += 1

84 elif num%6 == 3:85 low_quto.put(each)86 num += 1

87 elif num %6 == 4:88 close_quto.put(each)89 num +=1

90 elif num %6 == 5:91 update_time.put(each)92 num +=1

93 #while not

94 for each inresult:95 st = each.split('/')96 nation_que.put(st[len(st)-1])97

98 get_precent()99

100 defget_precent():101

102 while notnation_que.empty():103 if notupdate_time.empty():104 time_update =update_time.get(False)105 update_time.task_done()106 if notcurrent_quto.empty():107 new_rates =current_quto.get(False)108 current_quto.task_done()109 if notopen_quto.empty():110 opening =open_quto.get(False)111 open_quto.task_done()112 if nothigh_quto.empty():113 high =high_quto.get(False)114 high_quto.task_done()115 if notlow_quto.empty():116 low =low_quto.get(False)117 low_quto.task_done()118 if notclose_quto.empty():119 closing =close_quto.get(False)120 close_quto.task_done()121

122 ss =nation_que.get(False)123 printss124 printlow125 printhigh126 printtime_update127 printnew_rates128 printopening129

130 url = 'http://api.q.fx678.com/history.php?symbol=' + ss +'&limit=288&resolution=5&codeType=8100&st=0.8274405615006541'

131 printurl132 headers = {'Accept':'application/json, text/javascript, */*; q=0.01',133 'Accept-Encoding':'gzip, deflate',134 'Accept-Language':'zh-CN,zh;q=0.8',135 'Connection':'keep-alive',136 'Host':'api.q.fx678.com',137 'Origin':'http://quote.fx678.com',138 'Referer':'http://quote.fx678.com/symbol/USD',139 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'

140 }141 num_try = 2

142 while num_try >0:143 num_try -= 1

144 try:145 content = requests.get(url, headers=headers)146 html =json.loads(content.text)147 st = html['h']148 T_time = html['t']149 if len(st) > 0 and len(T_time) >0:150 draw_pict(ss,T_time,st,time_update,new_rates,opening,high,low,closing)151 break

152 excepturllib2.URLError as e:153 print 'Download error', e.reason154 nation_que.task_done()155 List =[]156 defdraw_pict(name,T_time1,high_rate,time_update,new_rate,opening,high,low,closing):157

158 High =T_time1159 Time =high_rate160 High_Rate =[]161 T_time =[]162 mmap = "{\"Type\":\"%s\",\"Current_quto\":\"%s\",\"Opening_quto\":\"%s\",\"High_quto\":\"%s\",\"low_quto\":\"%s\",\"Closing_quto\":\"%s\",\"Update_Time\":\"%s\",\"Real_TIme_infor\":{" %( name, new_rate, opening, high, low, closing, time_update)163 printmmap164 flag =0165 for each,high1 inzip(T_time1,high_rate):166 if flag == 1:167 mmap += ","

168 else:169 flag = 1

170 mm = "\"%s\":\"%s\""%(each,high1)171

172

173 st =time.localtime(float(each))174 mmap +=mm175 if st.tm_min ==0:176 T_time.append(st.tm_hour)177 High_Rate.append(high1)178 else:179 pass

180 mmap += "}}"

181 mmap1 =json.loads(mmap)182 printmmap1183 Share.insert(mmap1)184 if len(T_time) ==len(High_Rate):185 quick_sort(T_time,High_Rate,0,len(High_Rate)-1)186 List.append(High_Rate)187

188 defdraw_picture():189 colu =len(List)190

191 num = 1

192 for each inList:193 plt.subplot(colu/2 + 1,2,num)194 num+=1

195

196 list =each197 T_time =[]198 for i inrange(len(list)):199 T_time.append(i)200 printlen(list)201 printlen(T_time)202 plt.plot(T_time, list, marker='*')203

204 plt.show()205 plt.title('Share Message')206

207 if __name__ == '__main__':208 get_type_url()209 draw_picture()

37>25>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值