如何通过发新浪微博关闭电脑

      我们使用的是一种很笨的方法,但是,这是用python来自动化处理的。

1)配置信息:win7 64位+python3.3.2+pythonwin(下载地:http://sourceforge.net/projects/pywin32/files/pywin32/

2)资源手机微博客户端+网页微博+python代码+本地文件夹

     本文思想:先通过python代码把微博页面下载在电脑上,然后打开下载的微博主页,搜索匹配的微博,继而实行一

     些命令。

3)设置默认文件下载路径:用默认浏览器先跳转到你的微博主页(主页对于这个问题来说,比较容易),按下

      Ctrl+S,把你的微博主页下载到你的一个新建文件夹里,我的是 “D: est” ,下载好后,就把下载好的两个文件

   (一个html文件 +个文件夹)删掉

from win32 import win32api
import os,shutil,time,webbrowser,re

def shutnow():
    #模拟按键刷新网页,然后ctr+s+enter保存网页
    win32api.keybd_event(116,0,0,0)#F5
    time.sleep(5)
    win32api.keybd_event(17,0,0,0)#ctr
    #time.sleep(4)
    win32api.keybd_event(83,0,0,0)#s
    time.sleep(2)
    win32api.keybd_event(13,0,0,0)#enter
    time.sleep(3)
    #释放按键
    win32api.keybd_event(83,0,0x0002,0)
    win32api.keybd_event(17,0,0x0002,0)
    #time.sleep(4)
    s1=r'*********你的微博主页*********.htm'
    s2=r'*******跟着微博主页一起下载的文件夹********_files'
    time.sleep(3)
    #打开微博主页
    file=open(s1,encoding='utf-8').read()
    if re.search('#shutdown#',file):        
        #如果匹配的话,就关闭计算机
           o="c:\windows\system32\shutdown -p "
           os.system(o)
           return 1
    else:
        #不匹配的话,删除下载好的两个文件夹
        os.remove(s1)
        shutil.rmtree(s2)
        return 0

if __name__=='__main__':
    webbrowser.open_new_tab('http://weibo.com/kamsau/profile?rightmod=1&wvr=5&mod=personnumber')
    while True:            
        if shutnow()==1:
            break

5)代码缩小浏览器,接着,用python3.3.2打开新建的python文件,把这些代码复制

去,保存,按F5执行。接着用手机发一条微博,内容是” #shutdown#“,就可以了,在我的笔记本上行得通。

代码写得比较乱,没怎么改。

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/Rex7/p/4752593.html