上班时候想看股票行情怎么办?试试这个小例子,5分钟拉去一次股票价格,预警:

 #coding=utf-8
 import re
 import urllib2
 import time
 import threading

 import sys
 import os
 import struct
 import win32con
 import win32gui_struct

 from win32api import *
 try:
   from winxpgui import *
 except ImportError:
   from win32gui import *

 '''气泡显示逻辑'''

 class PyNOTIFYICONDATA:
   _struct_format = (
     "I" # DWORD cbSize; 结构大小(字节)
     "I" # HWND hWnd; 处理消息的窗口的句柄
     "I" # UINT uID; 唯一的标识符
     "I" # UINT uFlags;
     "I" # UINT uCallbackMessage; 处理消息的窗口接收的消息
     "I" # HICON hIcon; 托盘图标句柄
     "128s" # TCHAR szTip[128]; 提示文本
     "I" # DWORD dwState; 托盘图标状态
     "I" # DWORD dwStateMask; 状态掩码
     "256s" # TCHAR szInfo[256]; 气泡提示文本
     "I" # union {
         #   UINT  uTimeout; 气球提示消失时间(毫秒)
         #   UINT  uVersion; 版本(0 for V4, 3 for V5)
         # } DUMMYUNIONNAME;
     "64s" #    TCHAR szInfoTitle[64]; 气球提示标题
     "I" # DWORD dwInfoFlags; 气球提示图标
   )
   _struct = struct.Struct(_struct_format)

   hWnd = 0
   uID = 0
   uFlags = 0
   uCallbackMessage = 0
   hIcon = 0
   szTip = ''
   dwState = 0
   dwStateMask = 0
   szInfo = ''
   uTimeoutOrVersion = 0
   szInfoTitle = ''
   dwInfoFlags = 0

   def pack(self):
     return self._struct.pack(
       self._struct.size,
       self.hWnd,
       self.uID,
       self.uFlags,
       self.uCallbackMessage,
       self.hIcon,
       self.szTip,
       self.dwState,
       self.dwStateMask,
       self.szInfo,
       self.uTimeoutOrVersion,
       self.szInfoTitle,
       self.dwInfoFlags
     )

   def __setattr__(self, name, value):
     # avoid wrong field names
     if not hasattr(self, name):
       raise NameError, name
     self.__dict__[name] = value

 class MainWindow:
   def __init__(self, duration=3):
     # Register the Window class.
     wc = WNDCLASS()
     hinst = wc.hInstance = GetModuleHandle(None)
     wc.lpszClassName = "StockTask" # 字符串只要有值即可,下面3处也一样
     wc.lpfnWndProc = { win32con.WM_DESTROY: self.OnDestroy } # could also specify a wndproc.
     classAtom = RegisterClass(wc)

     # Create the Window.
     style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU
     self.hwnd = CreateWindow(classAtom, "GuTask Window", style,
       0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
       0, 0, hinst, None
     )
     UpdateWindow(self.hwnd)
     iconPathName = os.path.abspath('favicon.ico')
     print iconPathName
     icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
     try:
       hicon = LoadImage(hinst, iconPathName, win32con.IMAGE_ICON, 0, 0, icon_flags)
     except:
       hicon = LoadIcon(0, win32con.IDI_APPLICATION)
     flags = NIF_ICON | NIF_MESSAGE | NIF_TIP
     nid = (self.hwnd, 0, flags, win32con.WM_USER + 20, hicon, "Balloon  tooltip")
     Shell_NotifyIcon(NIM_ADD, nid)

   def show_balloon(self, title, msg,duration):
     # For this message I can't use the win32gui structure because
     # it doesn't declare the new, required fields
     nid = PyNOTIFYICONDATA()
     nid.hWnd = self.hwnd
     nid.uFlags = NIF_INFO

     # type of balloon and text are random
     nid.dwInfoFlags = NIIF_INFO
     nid.szInfo = msg[:64]
     nid.szInfoTitle = title[:256]

     # Call the Windows function, not the wrapped one
     from ctypes import windll
     Shell_NotifyIcon = windll.shell32.Shell_NotifyIconA
     Shell_NotifyIcon(NIM_MODIFY, nid.pack())
     time.sleep(duration)

   def OnDestroy(self, hwnd, msg, wparam, lparam):
     nid = (self.hwnd, 0)
     Shell_NotifyIcon(NIM_DELETE, nid)
     PostQuitMessage(0) # Terminate the app.

 tip_window=MainWindow()

 '''数据抓取逻辑'''
 class Stock:
     def __init__(self,code,price,warn_price):
         self.code=code
         self.price=price
         self.warn_price=warn_price

 watch_stocks=[]
 watch_stocks.append(Stock(',float('3.897'),float('3.897')))
 watch_stocks.append(Stock(',float('3.88'),float('3.88')))
 watch_stocks.append(Stock(',float('3.88'),float('3.88')))

 def spiderStockPrice(stocks):
     list=''
     for stock in stocks:
         code=stock.code
         ")):
             list+="s_sh"+code
         "):
             list+="s_sh"+code
         else:
             list+="s_sz"+code
         list+=","

     qUrl='http://hq.sinajs.cn/rn=1522216317579&list='+list

     # 获取行情数据
     markdes=urllib2.urlopen(qUrl).read()
     markdes=markdes.replace("var hq_str_s_sz","").replace("var hq_str_s_sh","").replace("=",",").replace("\"","").replace("\n","")

     stockMarkets=markdes.split(";")
     for stockMarket in stockMarkets:
         if(stockMarket!=''):
             sms=stockMarket.split(',')
             for stock in stocks:
                 if(stock.code==sms[0]):
                     stock.price=float(sms[2])
     return stocks

 def doSpider():
     current_stocks=spiderStockPrice(watch_stocks)
     for current_stock in current_stocks:
         if(current_stock.price*0.99<current_stock.warn_price):
             tip_window.show_balloon(current_stock.code,str(current_stock.price),5)

     global timer
     timer=threading.Timer(300,doSpider)
     timer.start()

 if(__name__=="__main__"):
     doSpider()

Python笔记(十一)——数据抓取例子的更多相关文章

  1. python&amp;php数据抓取、爬虫分析与中介,有网址案例

    近期在做一个网络爬虫程序.后台使用python不定时去抓取数据.前台使用php进行展示 站点是:http://se.dianfenxiang.com

  2. python 手机App数据抓取实战二抖音用户的抓取

    前言 什么?你问我国庆七天假期干了什么?说出来你可能不信,我爬取了cxk坤坤的抖音粉丝数据,我也不知道我为什么这么无聊. 本文主要记录如何使用appium自动化工具实现抖音App模拟滑动,然后分析数据 ...

  3. python 手机App数据抓取实战一

    前言 当前手机使用成为互联网主流,每天手机App产生大量数据,学习爬虫的人也不能只会爬取网页数据,我们需要学习如何从手机 APP 中获取数据,本文就以豆果美食为例,讲诉爬取手机App的流程 环境准备 ...

  4. python 3 Urllib 数据抓取

    1.0 Urllib简介 Urllib是python自带的标准库,无需安装,直接引用即可.urllib通常用于爬虫开发,API(应用程序编程接口)数据获取和测试.在python2和python3中,u ...

  5. python爬虫数据抓取方法汇总

    概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...

  6. Python爬虫工程师必学——App数据抓取实战 ✌✌

    Python爬虫工程师必学——App数据抓取实战 (一个人学习或许会很枯燥,但是寻找更多志同道合的朋友一起,学习将会变得更加有意义✌✌) 爬虫分为几大方向,WEB网页数据抓取.APP数据抓取.软件系统 ...

  7. 吴裕雄--天生自然python学习笔记:WEB数据抓取与分析

    Web 数据抓取技术具有非常巨大的应用需求及价值, 用 Python 在网页上收集数据,不仅抓取数据的操作简单, 而且其数据分析功能也十分强大. 通过 Python 的时lib 组件中的 urlpar ...

  8. 【Python入门只需20分钟】从安装到数据抓取、存储原来这么简单

    基于大众对Python的大肆吹捧和赞赏,作为一名Java从业人员,我本着批判与好奇的心态买了本python方面的书<毫无障碍学Python>.仅仅看了书前面一小部分的我......决定做一 ...

  9. 数据抓取分析(python + mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

随机推荐

  1. C#的WebBrowser操作frame

    刚学c#不久,也不太懂什么IHTMLDocument.IHTMLDocument2.IWebBrowser2等等.自己琢磨了好久,终于知道了怎么用WebBrowser操作frame和iframe. 1 ...

  2. html 表单赋值 和 时间戳 转换

    <script> window.onload = function () { var str; // console.log(@ViewBag.ID); $.post("/Ser ...

  3. PyCharm与GitHub环境配置

    转载地址:https://blog.csdn.net/xierhacker/article/details/70053162 一.准备工作 Ⅰ.git下载和安装 要连接GitHub,首先git是必不可 ...

  4. Node.js 常用Mongoose方法

    Node.js 手册查询-Mongoose 方法 一.Schema 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力.可以说是数据属性模型(传统意义的表结构 ...

  5. Python学习教程(Python学习视频_Python学些路线):Day06 函数和模块的使用

    Python学习教程(Python学习视频_Python学些路线):函数和模块的使用 在讲解本章节的内容之前,我们先来研究一道数学题,请说出下面的方程有多少组正整数解. $$x_1 + x_2 + x ...

  6. BZOJ 1060: [ZJOI2007]时态同步 树上问题 + 贪心

    Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数 字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路 ...

  7. Day 1 初识python

    1.Python简介 Python的历史 1989年圣诞节:Guido von Rossum开始写Python语言的编译器. 1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语 ...

  8. TensorFlow的序列模型代码解释(RNN、LSTM)---笔记(16)

    1.学习单步的RNN:RNNCell.BasicRNNCell.BasicLSTMCell.LSTMCell.GRUCell (1)RNNCell 如果要学习TensorFlow中的RNN,第一站应该 ...

  9. Lua的string库函数、lua中string的模式匹配

    --****************Lua的string库函数****************** --1.string.byte --string.byte (s [, i [, j]]) --取出 ...

  10. 【Educational Codeforces Round 53 (Rated for Div. 2) C】Vasya and Robot

    [链接] 我是链接,点我呀:) [题意] [题解] 如果|x|+|y|>n 显然.从(0,0)根本就没法到(x,y) 但|x|+|y|<=n还不一定就能到达(x,y) 注意到,你每走一步路 ...