上班时候想看股票行情怎么办?试试这个小例子,5分钟拉去一次股票价格,预警:
1 #coding=utf-8 2 import re 3 import urllib2 4 import time 5 import threading 6 7 import sys 8 import os 9 import struct 10 import win32con 11 import win32gui_struct 12 13 from win32api import * 14 try: 15 from winxpgui import * 16 except ImportError: 17 from win32gui import * 18 19 '''气泡显示逻辑''' 20 21 class PyNOTIFYICONDATA: 22 _struct_format = ( 23 "I" # DWORD cbSize; 结构大小(字节) 24 "I" # HWND hWnd; 处理消息的窗口的句柄 25 "I" # UINT uID; 唯一的标识符 26 "I" # UINT uFlags; 27 "I" # UINT uCallbackMessage; 处理消息的窗口接收的消息 28 "I" # HICON hIcon; 托盘图标句柄 29 "128s" # TCHAR szTip[128]; 提示文本 30 "I" # DWORD dwState; 托盘图标状态 31 "I" # DWORD dwStateMask; 状态掩码 32 "256s" # TCHAR szInfo[256]; 气泡提示文本 33 "I" # union { 34 # UINT uTimeout; 气球提示消失时间(毫秒) 35 # UINT uVersion; 版本(0 for V4, 3 for V5) 36 # } DUMMYUNIONNAME; 37 "64s" # TCHAR szInfoTitle[64]; 气球提示标题 38 "I" # DWORD dwInfoFlags; 气球提示图标 39 ) 40 _struct = struct.Struct(_struct_format) 41 42 hWnd = 0 43 uID = 0 44 uFlags = 0 45 uCallbackMessage = 0 46 hIcon = 0 47 szTip = '' 48 dwState = 0 49 dwStateMask = 0 50 szInfo = '' 51 uTimeoutOrVersion = 0 52 szInfoTitle = '' 53 dwInfoFlags = 0 54 55 def pack(self): 56 return self._struct.pack( 57 self._struct.size, 58 self.hWnd, 59 self.uID, 60 self.uFlags, 61 self.uCallbackMessage, 62 self.hIcon, 63 self.szTip, 64 self.dwState, 65 self.dwStateMask, 66 self.szInfo, 67 self.uTimeoutOrVersion, 68 self.szInfoTitle, 69 self.dwInfoFlags 70 ) 71 72 def __setattr__(self, name, value): 73 # avoid wrong field names 74 if not hasattr(self, name): 75 raise NameError, name 76 self.__dict__[name] = value 77 78 class MainWindow: 79 def __init__(self, duration=3): 80 # Register the Window class. 81 wc = WNDCLASS() 82 hinst = wc.hInstance = GetModuleHandle(None) 83 wc.lpszClassName = "StockTask" # 字符串只要有值即可,下面3处也一样 84 wc.lpfnWndProc = { win32con.WM_DESTROY: self.OnDestroy } # could also specify a wndproc. 85 classAtom = RegisterClass(wc) 86 87 # Create the Window. 88 style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU 89 self.hwnd = CreateWindow(classAtom, "GuTask Window", style, 90 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 91 0, 0, hinst, None 92 ) 93 UpdateWindow(self.hwnd) 94 iconPathName = os.path.abspath('favicon.ico') 95 print iconPathName 96 icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE 97 try: 98 hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags) 99 except: 100 hicon = LoadIcon(0, win32con.IDI_APPLICATION) 101 flags = NIF_ICON | NIF_MESSAGE | NIF_TIP 102 nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Balloon tooltip") 103 Shell_NotifyIcon(NIM_ADD, nid) 104 105 106 def show_balloon(self, title, msg,duration): 107 # For this message I can't use the win32gui structure because 108 # it doesn't declare the new, required fields 109 nid = PyNOTIFYICONDATA() 110 nid.hWnd = self.hwnd 111 nid.uFlags = NIF_INFO 112 113 # type of balloon and text are random 114 nid.dwInfoFlags = NIIF_INFO 115 nid.szInfo = msg[:64] 116 nid.szInfoTitle = title[:256] 117 118 # Call the Windows function, not the wrapped one 119 from ctypes import windll 120 Shell_NotifyIcon = windll.shell32.Shell_NotifyIconA 121 Shell_NotifyIcon(NIM_MODIFY, nid.pack()) 122 time.sleep(duration) 123 124 125 def OnDestroy(self, hwnd, msg, wparam, lparam): 126 nid = (self.hwnd, 0) 127 Shell_NotifyIcon(NIM_DELETE, nid) 128 PostQuitMessage(0) # Terminate the app. 129 130 tip_window=MainWindow() 131 132 133 '''数据抓取逻辑''' 134 class Stock: 135 def __init__(self,code,price,warn_price): 136 self.code=code 137 self.price=price 138 self.warn_price=warn_price 139 140 watch_stocks=[] 141 watch_stocks.append(Stock('510300',float('3.897'),float('3.897'))) 142 watch_stocks.append(Stock('000425',float('3.88'),float('3.88'))) 143 watch_stocks.append(Stock('601288',float('3.88'),float('3.88'))) 144 145 def spiderStockPrice(stocks): 146 list='' 147 for stock in stocks: 148 code=stock.code 149 if(code.startswith("6")): 150 list+="s_sh"+code 151 elif(code=="510300"): 152 list+="s_sh"+code 153 else: 154 list+="s_sz"+code 155 list+="," 156 157 qUrl='http://hq.sinajs.cn/rn=1522216317579&list='+list 158 159 # 获取行情数据 160 markdes=urllib2.urlopen(qUrl).read() 161 markdes=markdes.replace("var hq_str_s_sz","").replace("var hq_str_s_sh","").replace("=",",").replace("\"","").replace("\n","") 162 163 stockMarkets=markdes.split(";") 164 for stockMarket in stockMarkets: 165 if(stockMarket!=''): 166 sms=stockMarket.split(',') 167 for stock in stocks: 168 if(stock.code==sms[0]): 169 stock.price=float(sms[2]) 170 return stocks 171 172 def doSpider(): 173 current_stocks=spiderStockPrice(watch_stocks) 174 for current_stock in current_stocks: 175 if(current_stock.price*0.99<current_stock.warn_price): 176 tip_window.show_balloon(current_stock.code,str(current_stock.price),5) 177 178 global timer 179 timer=threading.Timer(300,doSpider) 180 timer.start() 181 182 if(__name__=="__main__"): 183 doSpider()