上班时候想看股票行情怎么办?试试这个小例子,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. Swift Pointer 使用指南

    Overview C Syntax Swift Syntax Note const Type * UnsafePointer<Type> 指针可变,指针指向的内存值不可变. Type * ...

  2. String类练习统计一个字符串中大小写字母及数字字符个数

    public class StringPractice { public static void main(String[] args) { //创建一个文本扫描器 Scanner sc = new ...

  3. 整理Crontab 定时计划

    一. 什么是crontab? crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和 ...

  4. jquery获取当前时间并且格式化

    Date.prototype.Format = function (fmt) {      var o = {          "M+": this.getMonth() + 1 ...

  5. 【剑指Offer】54、字符流中第一个不重复的字符

      题目描述:   请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字 ...

  6. (C/C++学习)1.C++中vector的使用

    说明:vector是C++中一个非常方便的容器类,它用于存放类型相同的元素,利用成员函数及相关函数可以方便的对元素进行增加或删除,排序或逆序等等,下面将对这些功能一一叙述. 一.vector的第一种用 ...

  7. 【hihocoder 1298】 数论五·欧拉函数

    [题目链接]:http://hihocoder.com/problemset/problem/1298 [题意] [题解] 用欧拉筛法; 能够同时求出1..MAX当中的所有质数和所有数的欧拉函数的值; ...

  8. 【微软2017年预科生计划在线编程笔试第二场 B】Diligent Robots

    [题目链接]:http://hihocoder.com/problemset/problem/1498 [题意] 一开始你有1个机器人; 你有n个工作; 每个工作都需要一个机器人花1小时完成; 然后每 ...

  9. 论文WAN Optimized Replication of Backup Datasets Using Stream-Informed Delta Compression

         这是EMC的备份小组发表在FAST12上的论文,主要是结合重删和差量数据压缩的方法,达到更高的数据压缩率.并且作者使用了一种基于数据流的差量数据压缩,消除了对索引的需求.通过测试达到的压缩效 ...

  10. LightOJ - 1232 - Coin Change (II)

    先上题目: 1232 - Coin Change (II)   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...