本人也是一枚苦逼的大学生,很不巧哈,接触了相关团支书的工作哈,由于学院抓青年大学习抓的比较严,时不时让团支书去更近一下同学们的进度,然后总会有那么几个同学忘记,这周是张三,李红,下一周可能就是王二麻子忘记做了。很不巧哈,他们的团支书刚刚学了一手粗制版的python,于是在此探讨一下!!!哈哈,废话就这么多,开始正题
首先声明:本小白代码,技术不是很专业,纯属分享探讨,大佬勿喷!!!
在写代码前,我参考了相关博客的代码,大致逻辑就是结合爬虫技术将青年大学习后台数据拿到进行对比,然后发邮件提醒,这个办法可行是可行,但是本人在亲测后发现还是有那么几个好学生老是因为学习忘记看邮箱,或者关了邮箱提醒什么的,或者是发邮件时邮箱被定义为垃圾邮件或者。。。。然后我就在想。。。还有没有其他方法。。。最后我找到了!!!不知不觉又说了一堆废话。哈哈,第一次写csdn 博客,嗯。。。嘻嘻!说正事啦说正事啦不然会被打啦
我结合前辈代码经验,用到selenium和appium相结合方法,selenium帮我将青年大学习数据获取下来,当然看自己实际情况吧,据我所了解每个地方后台不一样,你可以用其他方法,只要能够将同学们数据获取即可,至于方式可以多样。
from selenium import webdriver
import time
from lxml import etree
import requests
import smtplib
from email.mime.text import MIMEText
from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey
time_num = 600 #提醒间隔时间
send_time=600
fp = open('D:/青年大学习提醒/名单1.txt', 'w', encoding='utf-8')
fp.write('\n')
wd = webdriver.Chrome(executable_path='D:/chromedriver.exe')
wd.get('https://jxtw.h5yunban.cn/jxtw-qndxx/admin/login.php')
wd.find_element_by_id('LAY-user-login-username').send_keys('账号')
time.sleep(1)
wd.find_element_by_id('LAY-user-login-password').send_keys('密码')
wd.find_element_by_xpath('//*[@id="LAY-user-login"]/div[1]/div[2]/div[3]/button').click()
time.sleep(1)
wd.implicitly_wait(10)
wd.find_element_by_xpath('//*[@id="LAY-system-side-menu"]//a').click()
time.sleep(1)
wd.implicitly_wait(10)
i_frame = wd.find_element_by_class_name('layadmin-iframe')
wd.switch_to.frame(i_frame)
time.sleep(2)
wd.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/div[3]/div[1]//input').click()
wd.implicitly_wait(10)
wd.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/div[3]/div[1]//input').clear()
time.sleep(1)
wd.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/div[3]/div[1]/div/div/dl/dd[55]').click()
time.sleep(1)
wd.find_element_by_xpath('//a[1]').click()
time.sleep(1)
name_list = wd.find_elements_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[2]/div/div[2]/table/tbody/tr/td[6]')
time.sleep(1)
for name in name_list:
name_new = name.text
fp.write(name_new + '\n')
time.sleep(1)
wd.find_element_by_xpath('//a[2]').click()
time.sleep(1)
name_list = wd.find_elements_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[2]/div/div[2]/table/tbody/tr/td[6]')
time.sleep(1)
for name in name_list:
name_new = name.text
fp.write(name_new + '\n')
其实逻辑很简单就是将后台数据中已经完成了的人的名单下载下来,上边代码比较难看哈,由于本人 不是计算机专业的代码追求的是能跑即可,上边我写的代码主要是爬取后台名单然后保存到名单1.txt里边。
下面是我关于数据清洗对比那块的代码,主要是为了将李红啊张三的网名啥的转换为真实的名字啥的,太难了呀。。。
name_list = []
name_list1 = []
list_nofinish_name = []
list_nofinish_email = []
with open('D:/青年大学习提醒/名单.txt', 'r', encoding='utf_8') as fp:
list = fp.readlines()
for li in list:
li_1 = li.index('\n')
li_2 = li[0:li_1]
name_list.append(li_2)
with open('D:/青年大学习提醒/名单1.txt', 'r', encoding='utf_8') as fp1:
list1 = fp1.readlines()
for li1 in list1:
li1_1 = li1.index('\n')
li1_2 = li1[0:li1_1]
name_list1.append(li1_2)
i = 0
for name in name_list:
try:
name_list1.index(name)
# print(f'{name}已经完成!!')
except:
# print(f'{name}没有完成!!')
list_nofinish_name.append(name)
i = i + 1
if i == 0:
print('检查完毕,全体同学已经完成!!!')
else:
print(f'检查完毕,1班没有完成人数为: {i}人')
# print(list_nofinish_name)
list_email = []
with open('D:/青年大学习提醒/邮箱.txt', 'r', encoding='utf_8') as fp3:
e_mail = fp3.readlines()
for e in e_mail:
lisy = e.split()
name_1 = lisy[0][2:]
num = lisy[1] + '@qq.com'
list_new = [name_1, num]
list_email.append(list_new)
name_no=[]
qq_num_no=[]
for nofinish_name in list_nofinish_name:
for mess in list_email:
try:
mess.index(nofinish_name)
# print(f'mess:{mess}')
# print(f'nofinish_name:{nofinish_name}')
list_nofinish_email.append(mess[1])
e_mail_no=mess[1].replace('@qq.com','')
qq_num_no.append(e_mail_no)
if mess[0]=='2019041007000122':#这段代码意思是如果遇到网名为2019041007000122就把他改为王二麻子输出
name_no.append('王二麻子')
continue
if mess[0]=='2019041007000124':
name_no.append('李红')
continue
if mess[0]=='2019041007000110':
name_no.append('张三')
continue
else:
name_no.append(mess[0])
except:
pass
额。。。可能比较乱哈,本人还不太喜欢用函数封装,大家看思路即可哈哈,我的思路是将下载的名单1和自己创捷的名单进行对比筛选就可以啦.这些前辈门都有涉及的不难。
下面appium才是重头戏
def QQ_send(QQ, text, number):
desired_caps = {
"platformName": "Android",
"platformVersion": "7",
"deviceName": "127.0.0.1:62001",
"appPackage": "com.tencent.mobileqq",
"appActivity": ".activity.SplashActivity",
"noReset": True
}
desired_caps["unicodeKeyboard"] = "True"
desired_caps["resetKeyboard"] = "True"
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
el1 = driver.find_element_by_accessibility_id("搜索")
el1.click()
el2 = driver.find_element_by_id("com.tencent.mobileqq:id/et_search_keyword")
driver.implicitly_wait(7)
time.sleep(1)
el2.send_keys('班级群')
time.sleep(1)
driver.implicitly_wait(7)
el3 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.AbsListView/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.TextView[1]")
el3.click()
el1 = driver.find_element_by_accessibility_id("群聊设置")
el1.click()
el2 = driver.find_element_by_id("com.tencent.mobileqq:id/eom")
el2.click()
el3 = driver.find_element_by_accessibility_id("搜索")
el3.click()
el4 = driver.find_element_by_id("com.tencent.mobileqq:id/et_search_keyword")
el4.send_keys(QQ)
el3 = driver.find_element_by_id("com.tencent.mobileqq:id/dvo")
el3.click()
el4 = driver.find_element_by_accessibility_id("发消息")
el4.click()
for i in range(number):
el5 = driver.find_element_by_id("com.tencent.mobileqq:id/input")
el5.click()
el6 = driver.find_element_by_id("com.tencent.mobileqq:id/input")
time.sleep(1)
el6.send_keys(text)
driver.implicitly_wait(7)
time.sleep(1)
el7 = driver.find_element_by_id("com.tencent.mobileqq:id/fun_btn")
el7.click()
driver.implicitly_wait(3)
driver.back()
这是我写的封装函数,主要定义了一个QQ_send(qq,text,number),干嘛用的捏,上边不是已经筛选出名字了嘛,接下来就是根据这些没有完成的同学进行在线对话框提醒。appium跟selenium差不多,在电脑上装一个模拟器然后驱动模拟器登录小号充当机器人即可
下边是完整代码:
# -*- codeing = utf-8 -*-
# @Time : 2021/10/28 0:57
# @Author :
# @File : 青年大学习QQ提醒接口.py
# @software: PyCharm
# -*- coding:utf-8 -*-
from selenium import webdriver
import time
from lxml import etree
import requests
import smtplib
from email.mime.text import MIMEText
from appium import webdriver
from appium.webdriver.extensions.android.nativekey import AndroidKey
time_num = 600 #提醒间隔时间
send_time=600
fp = open('D:/青年大学习提醒/名单1.txt', 'w', encoding='utf-8')
fp.write('\n')
wd = webdriver.Chrome(executable_path='D:/chromedriver.exe')
wd.get('https://jxtw.h5yunban.cn/jxtw-qndxx/admin/login.php')
wd.find_element_by_id('LAY-user-login-username').send_keys('账号')
time.sleep(1)
wd.find_element_by_id('LAY-user-login-password').send_keys('密码')
wd.find_element_by_xpath('//*[@id="LAY-user-login"]/div[1]/div[2]/div[3]/button').click()
time.sleep(1)
wd.implicitly_wait(10)
wd.find_element_by_xpath('//*[@id="LAY-system-side-menu"]//a').click()
time.sleep(1)
wd.implicitly_wait(10)
i_frame = wd.find_element_by_class_name('layadmin-iframe')
wd.switch_to.frame(i_frame)
time.sleep(2)
wd.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/div[3]/div[1]//input').click()
wd.implicitly_wait(10)
wd.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/div[3]/div[1]//input').clear()
time.sleep(1)
wd.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/div[3]/div[1]/div/div/dl/dd[55]').click()
time.sleep(1)
wd.find_element_by_xpath('//a[1]').click()
time.sleep(1)
name_list = wd.find_elements_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[2]/div/div[2]/table/tbody/tr/td[6]')
time.sleep(1)
for name in name_list:
name_new = name.text
fp.write(name_new + '\n')
time.sleep(1)
wd.find_element_by_xpath('//a[2]').click()
time.sleep(1)
name_list = wd.find_elements_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[2]/div/div[2]/table/tbody/tr/td[6]')
time.sleep(1)
for name in name_list:
name_new = name.text
fp.write(name_new + '\n')
name_list = []
name_list1 = []
list_nofinish_name = []
list_nofinish_email = []
with open('D:/青年大学习提醒/名单.txt', 'r', encoding='utf_8') as fp:
list = fp.readlines()
for li in list:
li_1 = li.index('\n')
li_2 = li[0:li_1]
name_list.append(li_2)
with open('D:/青年大学习提醒/名单1.txt', 'r', encoding='utf_8') as fp1:
list1 = fp1.readlines()
for li1 in list1:
li1_1 = li1.index('\n')
li1_2 = li1[0:li1_1]
name_list1.append(li1_2)
i = 0
for name in name_list:
try:
name_list1.index(name)
# print(f'{name}已经完成!!')
except:
# print(f'{name}没有完成!!')
list_nofinish_name.append(name)
i = i + 1
if i == 0:
print('检查完毕,全体同学已经完成!!!')
else:
print(f'检查完毕,1班没有完成人数为: {i}人')
# print(list_nofinish_name)
list_email = []
with open('D:/青年大学习提醒/邮箱.txt', 'r', encoding='utf_8') as fp3:
e_mail = fp3.readlines()
for e in e_mail:
lisy = e.split()
name_1 = lisy[0][2:]
num = lisy[1] + '@qq.com'
list_new = [name_1, num]
list_email.append(list_new)
name_no=[]
qq_num_no=[]
for nofinish_name in list_nofinish_name:
for mess in list_email:
try:
mess.index(nofinish_name)
# print(f'mess:{mess}')
# print(f'nofinish_name:{nofinish_name}')
list_nofinish_email.append(mess[1])
e_mail_no=mess[1].replace('@qq.com','')
qq_num_no.append(e_mail_no)
if mess[0]=='2019041007000122':
name_no.append('王二麻子')
continue
if mess[0]=='2019041007000124':
name_no.append('张三')
continue
if mess[0]=='2019041007000110':
name_no.append('李红')
continue
else:
name_no.append(mess[0])
except:
pass
if len(name_no)>0:
print(name_no)
# print(len(name_no))
# print(list_nofinish_email)
# print(qq_num_no)
def QQ_send(QQ, text, number):
desired_caps = {
"platformName": "Android",
"platformVersion": "7",
"deviceName": "127.0.0.1:62001",
"appPackage": "com.tencent.mobileqq",
"appActivity": ".activity.SplashActivity",
"noReset": True
}
desired_caps["unicodeKeyboard"] = "True"
desired_caps["resetKeyboard"] = "True"
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(5)
el1 = driver.find_element_by_accessibility_id("搜索")
el1.click()
el2 = driver.find_element_by_id("com.tencent.mobileqq:id/et_search_keyword")
driver.implicitly_wait(7)
time.sleep(1)
el2.send_keys('班级群')
time.sleep(1)
driver.implicitly_wait(7)
el3 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.AbsListView/android.widget.LinearLayout[2]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.TextView[1]")
el3.click()
el1 = driver.find_element_by_accessibility_id("群聊设置")
el1.click()
el2 = driver.find_element_by_id("com.tencent.mobileqq:id/eom")
el2.click()
el3 = driver.find_element_by_accessibility_id("搜索")
el3.click()
el4 = driver.find_element_by_id("com.tencent.mobileqq:id/et_search_keyword")
el4.send_keys(QQ)
el3 = driver.find_element_by_id("com.tencent.mobileqq:id/dvo")
el3.click()
el4 = driver.find_element_by_accessibility_id("发消息")
el4.click()
for i in range(number):
el5 = driver.find_element_by_id("com.tencent.mobileqq:id/input")
el5.click()
el6 = driver.find_element_by_id("com.tencent.mobileqq:id/input")
time.sleep(1)
el6.send_keys(text)
driver.implicitly_wait(7)
time.sleep(1)
el7 = driver.find_element_by_id("com.tencent.mobileqq:id/fun_btn")
el7.click()
driver.implicitly_wait(3)
driver.back()
text=[]
for name_no_1 in name_no:
text_1=f'尊敬的1班{name_no_1}同学,您好:\n 请您尽快完成青年大学习的学习!!!\n 请您尽快完成青年大学习的学习!!!\n ' \
'请您尽快完成青年大学习的学习!!!\n \n \n \n 学习青年大学习是让每一位青年进行学习,提高思想素质,热爱自己的国家,' \
'关心国家大事,争取为了国家繁荣富强而努力奋斗。学习是永无止境的,特别是趁青春年少时,更应当紧紧抓住时间,努力学习,为以后工作提供知识储备' \
',永无止境的学习会在不知不觉中改变自己的命运!!!'
text.append(text_1)
if len(list_nofinish_email) > 0:
i=0
for i in range(len(name_no)):
QQ_send(QQ=qq_num_no[i],text=text[i],number=3)#number是发送消息的条数
print(f'已经提醒{name_no[i]}去完成青年大学习!!!')
i + 1
主要是跟大家探讨学习,这代码还有很多改进的地方,请对一名三个月上手python非计算机专业小白手下留情