zoukankan      html  css  js  c++  java
  • Python实现利用微博远程关机

    利用爬虫,定时获取微博内容。用正则匹配指令

    #检测微博内容实现自动操作电脑
    #2015/3/16
    
    import os
    import re
    import urllib.request
    import time
    
    class sCrapy:
        def __init__(self,url):
            self.url = url
    
        def get_html(self):
            self.req = urllib.request.Request(self.url)
            self.req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36')
            self.html = urllib.request.urlopen(self.req).read().decode("utf-8")
    
        def find_message(self,Regex = None,target=None):
            print("I AM Online")
            self.Getlist = re.search(Regex,self.html)
            self.get_instruction = self.Getlist.groups()
            self.get_time = False
            if re.search(r"刚刚",self.html):
                self.get_time = True
                print("right now matched")
            elif re.search(r"[0-9]{1,2}分钟前",self.html):
                self.get_time = True
                print("minute matched")
    
            #get_time = re.search(r"1分钟前",self.html) if re.search("刚刚")
            #print(get_time)
            print("Before run the get_time is ",self.get_time)
            if re.search(r'shutdown',self.get_instruction[0]) and self.get_time:
                #print("Before judge the shutdown_a is ",shutdown_a)
                #print("
    Before judge the shutdown_t is ",shutdown_t)
                print("I search the %s"%self.get_instruction[0])
    
                if re.search(r"-a",self.get_instruction[0]) and target[0] == False:
                    print("取消关机")
                    os.system("shutdown -a")
                    self.get_time = False
                    target[0] = True
                    target[1] = False
                    print("Now the get_time is ",self.get_time)
                elif target[1] == False and re.search(r"-t",self.get_instruction[0]):
                    self.time = str(re.findall(r"[0-9]+",self.get_instruction[0])[0])
                    self.shutdown = "shutdown "+"-s "+"-t "+self.time
                    print("亲,要关机了哟")
                    #print(shutdown)
                    os.system(self.shutdown)
                    self.get_time = False
                    target[1] = True
                    target[0] = False
                    print("Now the get_time is ",self.get_time)
            
    
    
    
    
            
    
    
    
    
    if __name__ == '__main__':
        url = "http://t.qq.com/devil19951104"
        Craw = sCrapy(url)
        target = []
        target.append(False)
        target.append(False)
        #target = False
        while True :
            Craw.get_html()
            try:
                Craw.find_message(r'([a-z]+ -[a-zA-Z]{1} {0,1}[0-9]*)',target)
            except:
                print("亲,还没有命令或者你的命令已经开始执行啦~~")
            print("Already")
            time.sleep(600)
  • 相关阅读:
    shell入门-sed-2替换功能
    shell入门-sed-1
    shell入门-grep-3-egrep
    shell入门-grep2
    shell入门-grep过滤-1
    shell入门-连接符(并且、和、或者)
    shell入门-tr替换字符和split切割大文件
    shell入门-uniq去重复和tee重定向
    class类的相关操作 --| 公有普通方法 | 私有普通方法 | 无参方法 | 有参方法
    类的封装性-- | 成员属性 | 成员方法 | 私有属性 | 私有方法 之间调用
  • 原文地址:https://www.cnblogs.com/You0/p/4459810.html
Copyright © 2011-2022 走看看