老虎证券爬取港股(对小米股价的实时显示和报警)

比如小米的实时股价

from bs4 import BeautifulSoup
import requests

url="https://www.laohu8.com/stock/010810"
html=requests.get(url)//获取网页
a=soup.select('.stock-price .latest')//在网页中查找价格
print(a[0].text)//打印小米实时股价

以下是全部代码:包括设置tkinter UI显示小米实时股价和计算盈利百分比,并设定警戒值并报警,并实时隐藏图标

from tkinter import StringVar
import tkinter.messagebox   
import threading
from threading import Lock
##import mysql.connector
##```
lock = Lock()
HideBusyFalg=False
class GupiaoDemo:
    def __init__(self):
        self.window = tk.Tk() # 第1步,实例化object,建立窗口self.window
        # 第2步,给窗口的可视化起名字
        self.window.title('EDM')
        # 第3步,设定窗口的大小(长 * 宽)
        self.window.geometry('100x100')  # 这里的乘是小x
        self.window.bind(sequence="", func=self.processMouseEnterEvent)
        self.window.bind(sequence="", func=self.processMouseLeaveEvent)
        v = StringVar()
        v2 = StringVar()
        # 第4步,在图形界面上设定标签
        l = tk.Label(self.window,textvariable=v, bg='green', font=('Arial', 12), width=10, height=2)
        # 说明: bg为背景,font为字体,width为长,height为高,这里的长和高是字符的长和高,比如height=2,就是标签有2个字符这么高

        # 第5步,放置标签
        l.pack()    # Label内容content区域放置位置,自动调节尺寸
        # 放置lable的方法有:1)l.pack(); 2)l.place();

        l2 = tk.Label(self.window,textvariable=v2, bg='green', font=('Arial', 12), width=10, height=2)
        l2.pack()

        v.set("01810")
        v2.set("01060")
        i=0

        //爬取实时股票
        def Get(socoktcode):  
        #html文本
            url="https://www.laohu8.com/stock/"+str(socoktcode) #小米是:01810
            html=requests.get(url)
            soup=BeautifulSoup(html.text,features="lxml")
            #  'a'    a标签
            #  '.a'   class='a'
            #  .get('属性')
            a=soup.select('.stock-price .latest')
            return a[0].text

        //报警设置
        def showWarning(s):
            def MessageBox(ss):
                tkinter.messagebox.showwarning(title="Believe me", message=str(ss)+"It's Time Now!")
            th = threading.Thread(target=MessageBox,args=(s,))
            th.setDaemon(True)
            th.start()
    
    

        def show():
            nonlocal v,v2,l,l2,i
            while True:
##                try:
                    xiaomi=Get("01810") //获取小米股价
                    aliyingye=Get("01060")//获取阿里影业股价
##        xiaomi=str(int(l.cget("text"))+1)
##        aliyingye=str(int(l2.cget("text"))+1)

        
                    if(float(xiaomi)>9.065)://假如你买入时9.065,大于它显示绿色,小于它小时红色看你的习惯()
                        l.config(bg="green")
                    elif (float(xiaomi)<9.065):
                        l.config(bg="red")
                    else:
                        l.config(bg="gray")

                    percent=(float(xiaomi)-9.065)*100/9.065//显示盈利百分比

                    if(float(xiaomi)>13.40 or float(xiaomi)<8.0)://设置心理报警值,高于或低于这个值弹窗提醒
                        showWarning(float(xiaomi))

            

            

        
                    if(float(aliyingye)>1.292):
                        l2.config(bg="green")
                    elif (float(aliyingye)<1.292):
                        l2.config(bg="red")
                    else:
                        l2.config(bg="gray")


##                    if(float(aliyingye)>1.4 or float(aliyingye)<0.9):
##                        showWarning(float(aliyingye))


                
                    i+=1
                    print(i)
        
                    v.set("{:.2f} {:.2f}%".format(float(xiaomi),percent))
                    v2.set(aliyingye)          
##                except:
##                    time.sleep(1200)
##                    print(i)
##                    print("Error")
##                else:
                    time.sleep(10)
        th = threading.Thread(target=show)
        th.setDaemon(True)
        th.start()


        self.window.mainloop()


        
        
 
    def processMouseEnterEvent(self, me): //用来将UI实时隐藏,鼠标滑过就显示出来,鼠标退出则隐藏,非常方便
        print("MouseIn")
        def AdjustX(window):
            
            global lock,HideBusyFalg
            if(HideBusyFalg):
                return           
            lock.acquire()
            HideBusyFalg=True
            
            y=window.winfo_y()
            while(y<0):
                ww = window.winfo_width()
                wh = window.winfo_height()

                x = 0+window.winfo_x()
                y = y+2
                window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
                time.sleep(0.01)
                print("%dx%d+%d+%d" %(ww,wh,x,y))
            HideBusyFalg=False
            lock.release()
            
        th = threading.Thread(target=AdjustX,args=(self.window,))
        th.setDaemon(True)
        th.start()
        
        

    def processMouseLeaveEvent(self, me):     
        print("MouseOut")

        def AdjustX(window):
            
            global lock,HideBusyFalg
            if(HideBusyFalg):
                return          
            lock.acquire()
            HideBusyFalg=True
            
            y=window.winfo_y()
            while(y>0-self.window.winfo_height()):
                ww = window.winfo_width()
                wh = window.winfo_height()

                x = 0+window.winfo_x()
                y = y-2
                window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
                time.sleep(0.01)
                print("%dx%d+%d+%d" %(ww,wh,x,y))
            HideBusyFalg=False
            lock.release()
            
        th = threading.Thread(target=AdjustX,args=(self.window,))
        th.setDaemon(True)
        th.start()

GupiaoDemo()

非常方便哦 只是还没有日K,5日K等 之后会更新

你可能感兴趣的:(Python)