学了一个礼拜Python之后写的,代码很粗糙,只是为了完成利用163邮箱远程关电脑功能。直接把代码发上来吧。要执行的话得先安装一些模块,看import语句。
十月初写的,写完这个之后就没怎么写python了,忘得厉害。
halla.py
import requestsimport mechanicalsoupimport bs4import reimport TESTimport timeimport argparsefrom datetime import datetimeimport lxmlparser = argparse.ArgumentParser(description='Login to GitHub.')parser.add_argument("username")parser.add_argument("password")args = parser.parse_args()n = 0startime = datetime.strptime(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'%Y-%m-%d %H:%M:%S')while True: #打开“浏览器” browser = mechanicalsoup.Browser() #第一个页面url url = "http://reg.163.com/logins.jsp?type=1&product=mail163&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D1" #打开网易登录页面 login_page = browser.get(url) #输入账户密码然后提交表单 login_form = login_page.soup.select("#fLogin")[0] login_form.select("#username")[0]['value'] = args.username login_form.select("#password")[0]['value'] = args.password page2 = browser.submit(login_form, login_page.url) #获取page2内容 page2_soup = bs4.BeautifulSoup(page2.content) #用page2的cookies登录网易邮箱 cookie = page2.cookies.get_dict() page3 = browser.get('http://entry.mail.163.com/coremail/fcg/ntesdoor2') #获取page3的url中的sid,给出XMLurl sid = re.findall(r'(?<==).*(?=&)',page3.url) XMLurl = 'http://mail.163.com/js6/s?sid='+sid[0]+'&func=mbox:listMessages&topNav_mobileIcon_show=1&TopTabReaderShow=1&TopTabLofterShow=1&welcome_welcomemodule_mailrecom_click=1&LeftNavfolder1Click=1&mbox_folder_enter=1' #打开XMLurl页面,获取邮件 page4 = browser.post(XMLurl) with open(r'q.xml','w',encoding='utf-8') as q: q.write(page4.text) TEST.PPP(r'q.xml',startime).ppp() n+=1 print("循环执行第"+str(n)+'次') time.sleep(30)
TEST.py
import lxml.etreeimport osfrom datetime import datetimeclass PPP(): def __init__(self, src, startime): self.src = src self.startime = startime def ppp(self): parser = lxml.etree.XMLParser(recover=True) tree = lxml.etree.parse(self.src, parser) root = tree.getroot() array = root.find('array') for objects in array: FLAG = False for items in objects: if items.get('name') == 'subject': if items.text == '999': FLAG = True if items.get('name') == 'sentDate': raw_sentData = items.text sentData = datetime.strptime(raw_sentData,'%Y-%m-%d %H:%M:%S') if self.startime<sentData and FLAG==True: print('shutdown -s -t 60')
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VEVB武林网。
新闻热点
疑难解答