1. 程式人生 > 其它 >全球某工商雲戰役自動打卡系統

全球某工商雲戰役自動打卡系統

技術標籤:pythontkinterlinuxhttps

文章目錄


前言

考試周不想複習,自學python,看了一個下午毫無感覺,於是想著實操一下,試試能不能搞個雲戰役自動打卡,搞到凌晨兩點左右成功實現了使用python批量打卡,後來不斷改進增加功能,大致實現了使用者的新增每日自動批量打卡以及每日打卡成功之後的資訊報送,不過仍有很多不足的地方還需改進


一、python連線打卡網站實現自動批量打卡

1.程式碼

程式碼如下(示例):

import requests
import time
url='https://nco.zjgsu.edu.cn/genqrcode/2019032'
file = open("C:/Users/Administrator/Desktop/python檔案/spide/helloSpide.txt", "r")
file_read = file.readlines()#讀取檔案內容(以列表形式)
lenthOfFile = len(file_read)#確定長度
for i in range
(0, lenthOfFile, 4):#這裡是4後邊增加了郵箱資訊之後會變成5 # 每一個for迴圈簽到一次 file_read_name = file_read[i]#讀取使用者名稱(學號) file_read_name = file_read_name.strip()#將讀取的使用者名稱去空格及回車 file_read_pass = file_read[i+1]#讀取密碼 file_read_pass = file_read_pass.strip() file_read_first_location = file_read[i+2]#讀取省市 file_read_first_location =
file_read_first_location.strip() file_read_last_location = file_read[i+3]#讀取地區 file_read_last_location = file_read_last_location.strip() # 讀取檔案內容並存儲 # 以下是第一個介面 myHeaders_1 = { 'User-Agent': 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02 '#頁面只允許手機端訪問於是更改頭部為手機可以直接網上搜代理(這一步很關鍵) } url = 'https://nco.zjgsu.edu.cn/genqrcode/2019032' r3 = requests.post(url, headers=myHeaders_1) # 向https://nco.zjgsu.edu.cn/genqrcode/2019032傳送請求(進入登入介面) yourLocal1 = file_read_first_location # 讀取現在地址的首地址(例如浙江省杭州市) yourLocal2 = file_read_last_location # 讀取具體地址 myHeaders_2 = { 'Upgrade-Insecure-Requests': '1', 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': 'zjgsusessionsid=s%3AGSYaaVwF_2pedsizsHYTYSY64hVl3vL_.NMnLf1bV1PJSAS%2FeZXUkcyPkFpBq%2BcMVQYq4XPHXAAU; _ncov_uuid=a59e0ac8-da06-4fb1-83d8-000976c317a9; _ncov_username=1910080117; _ncov_psswd=09187X', 'User-Agent': 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02 ' } myHeaders_2['Cookie'] = r3.headers['set-cookie']#將獲得的cookie傳到下一個檔案頭(進行cookie的替換) payload = { 'name': ' ', 'psswd': ' ' } payload['name'] = file_read_name#設定post引數 payload['psswd'] = file_read_pass#設定post引數 url = 'https://nco.zjgsu.edu.cn/login' r=requests.post(url, data=payload, headers=myHeaders_2)#登入 # r.encoding='utf-8' print(r.text) print(r.headers) time.sleep(10) # 介面跳轉休息十秒怕被抓 myHeaders_3 = { 'Upgrade-Insecure-Requests': '1', 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': 'zjgsusessionsid=s%3AOIlBmUda3tnT-fWds6uWEKKADgTt3IAd.zQ1LcseRzDNFPv5EU1AlkyyWeSMz6424x6HK8cbov8Q; _ncov_uuid=03d31c9b-696f-4429-a0ca-6e2b7a2e10ce; _ncov_username=1910080117; _ncov_psswd=09187X', 'User-Agent': 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02 ' } myHeaders_3['Cookie'] = r.headers['set-cookie']#傳遞cookie payload2 = { 'uuid': 'a59e0ac8-da06-4fb1-83d8-000976c317a9', 'locationInfo': '%E6%B5%99%E6%B1%9F%E7%9C%81%E6%9D%AD%E5%B7%9E%E5%B8%82', 'currentResd': '%E6%B5%99%E6%B1%9F%E7%9C%81%E6%B8%A9%E5%B7%9E%E5%B8%82%E9%BE%99%E6%B8%AF%E5%B8%82%E4%B8%B4%E6%B8%AF%E8%B7%AF862%E5%8F%B7', 'fromHbToZjDate': '', 'fromHbToZj': 'C', 'fromWtToHzDate': '', 'fromWtToHz': 'B', 'meetDate': '', 'meetCase': 'C', 'travelDate': '', 'travelCase': 'D', 'medObsvReason': '', 'medObsv': 'B', 'belowCaseDesc': '', 'belowCase': 'D', 'temperature': '', 'notApplyReason': '', 'hzQRCode': 'A', 'specialDesc': '' } payload2['locationInfo'] = yourLocal1 # 換地址 payload2['currentResd'] = yourLocal2 # 換地址 url2 = 'https://nco.zjgsu.edu.cn/' r2 = requests.post(url2, data=payload2, headers=myHeaders_2)#打卡成功 # r.encoding='utf-8' print(r2.text) time.sleep(5) # 完成一個人的休眠五秒然後繼續打卡下一個人

2.關鍵部分分析

程式碼如下(示例):

 r=requests.post(url, data=payload, headers=myHeaders_2)

前面的程式碼都是圍繞著這一部分程式碼展開的,構造url也號payload也好headers也好,只要構造得當便可實現想要的功能
例如:
前面只能用手機端登入打卡介面,於是將headers中的user-agent更改為安卓端的user-agent便可

myHeaders_1['User-Agent'] = 'xxxxxxxx'

二、定時啟動打卡程式

1、騰訊伺服器

顯然我的電腦不能一直開著,所以和室友花重金(可憐學生黨)買了騰訊伺服器。
騰訊云云伺服器 (Cloud Virtual Machine,CVM) 是在雲中提供可擴充套件的計算服務。 [1] 騰訊雲在亞太雲端計算市場份額位列第四,首次超過谷歌。

2、使用linux+crontab新增定時啟動項

忘記crontab是不是自帶的了,如果要安裝

yum install vixie-cron
yum install crontabs

之後新增定時啟動項,不過在這之前要先熟悉一下crontab的使用
開啟crontab

crontab -e

這時會進入vi編輯器模式,然後在新的一行新增你你想要的定時任務,不過這要熟悉cron表示式的語法例如每天0點啟動

0 0 * * * 程式名 >>想記錄的日誌名

我crontab裡面的內容:

2 0 * * * python /root/ShareFile/myFirstSpide.py >>/root/ShareFile/EverDay.txt

代表著每天0:02使用python開啟myFirstSpide.py並將執行記錄寫入EverDay.txt

三、每日報送成功傳送郵件

1、傳送郵件功能

這裡是利用smtp來發送,使用smtp時需要在你的郵箱設定裡開啟此功能,並獲得動態密碼

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# from email.mime.image import MIMEImage
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com', 25)#這裡使用163來發送郵件qq的話改為smtp.qq.com
username = 'xxxxxxxx'#你的郵箱名
password = 'xxxxxxxx'#你的動態密碼(注意這裡不是你的密碼而是你在163開啟的smtp的動態碼)
smtp.login(username, password)
sender = 'xxxxxxx'#傳送人郵箱名
receiver = 'xxxxxxx'#接收者郵箱
#傳送郵件文字內容
subject = 'pythonmailtest'#檔案內容
msg = MIMEMultipart('mixed') 
msg['Subject'] = subject
msg['From'] = '[email protected] <[email protected]>'
msg['To'] = '[email protected]'
text = 'Hello World'#檔案內容
text_plain = MIMEText(text, 'plain', 'utf-8')#郵件內容
msg.attach(text_plain)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

2、結合打卡,完成每日打卡成功通知

import requests
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
url='https://nco.zjgsu.edu.cn/genqrcode/2019032'

#傳送郵件程式
def sentSecessMsg(receiver):
    smtp = smtplib.SMTP()
    smtp.connect('smtp.qq.com', 25)
    username = 'xxxxxxx'
    password = 'xxxxxxx'
    smtp.login(username, password)
    sender = 'xxxxxxx'
    #傳送郵件文字內容
    subject = '哈嘍哈:今日報送成功'
    msg = MIMEMultipart('mixed') 
    msg['Subject'] = subject
    msg['From'] = '[email protected] <[email protected]>'
    msg['To'] = receiver
    text = '打卡成功'
    text_plain = MIMEText(text, 'plain', 'utf-8')
    msg.attach(text_plain)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()


file = open("C:/Users/Administrator/Desktop/python檔案/spide/helloSpide.txt", "r")
file_read = file.readlines()
lenthOfFile = len(file_read)
for i in range(0, lenthOfFile, 5):
    # 每一個for迴圈簽到一次
    file_read_name = file_read[i]
    file_read_name = file_read_name.strip()
    file_read_pass = file_read[i+1]
    file_read_pass = file_read_pass.strip()
    file_read_first_location = file_read[i+2]
    file_read_first_location = file_read_first_location.strip()
    file_read_last_location = file_read[i+3]
    file_read_last_location = file_read_last_location.strip()
    # 讀取檔案內容並存儲
    # 以下是第一個介面
    myHeaders_1 = {
        'User-Agent': 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02 '#頁面只允許手機端訪問於是更改頭部為手機
    }
    url = 'https://nco.zjgsu.edu.cn/genqrcode/2019032'
    r3 = requests.post(url, headers=myHeaders_1)  # 向https://nco.zjgsu.edu.cn/genqrcode/2019032傳送請求

    yourLocal1 = file_read_first_location  # 讀取現在地址的首地址(例如浙江省杭州市)
    yourLocal2 = file_read_last_location  # 讀取具體地址
    myHeaders_2 = {
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie': 'zjgsusessionsid=s%3AGSYaaVwF_2pedsizsHYTYSY64hVl3vL_.NMnLf1bV1PJSAS%2FeZXUkcyPkFpBq%2BcMVQYq4XPHXAAU; _ncov_uuid=a59e0ac8-da06-4fb1-83d8-000976c317a9; _ncov_username=1910080117; _ncov_psswd=09187X',
        'User-Agent': 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02 '
        
    }
    myHeaders_2['Cookie'] = r3.headers['set-cookie']#將獲得的cookie傳到下一個檔案頭
    payload = {
        'name': ' ', 'psswd': ' '
    }
    payload['name'] = file_read_name
    payload['psswd'] = file_read_pass
    url = 'https://nco.zjgsu.edu.cn/login'
    r=requests.post(url, data=payload, headers=myHeaders_2)
    # r.encoding='utf-8'
    print(r.text)
    print(r.headers)
    # print(r.headers['Url-Hash'])
    # https://blog.csdn.net/sinat_32258909/article/details/52404305
    time.sleep(10)  # 介面跳轉休息十秒

    myHeaders_3 = {
        'Upgrade-Insecure-Requests': '1',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie': 'zjgsusessionsid=s%3AOIlBmUda3tnT-fWds6uWEKKADgTt3IAd.zQ1LcseRzDNFPv5EU1AlkyyWeSMz6424x6HK8cbov8Q; _ncov_uuid=03d31c9b-696f-4429-a0ca-6e2b7a2e10ce; _ncov_username=1910080117; _ncov_psswd=09187X',
        'User-Agent': 'Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02 '
        
    }
    myHeaders_3['Cookie'] = r.headers['set-cookie']
    payload2 = {
        'uuid': 'a59e0ac8-da06-4fb1-83d8-000976c317a9',
        'locationInfo': '%E6%B5%99%E6%B1%9F%E7%9C%81%E6%9D%AD%E5%B7%9E%E5%B8%82',
        'currentResd': '%E6%B5%99%E6%B1%9F%E7%9C%81%E6%B8%A9%E5%B7%9E%E5%B8%82%E9%BE%99%E6%B8%AF%E5%B8%82%E4%B8%B4%E6%B8%AF%E8%B7%AF862%E5%8F%B7',
        'fromHbToZjDate': '',
        'fromHbToZj': 'C',
        'fromWtToHzDate': '',
        'fromWtToHz': 'B',
        'meetDate': '',
        'meetCase': 'C',
        'travelDate': '',
        'travelCase': 'D',
        'medObsvReason': '',
        'medObsv': 'B',
        'belowCaseDesc': '',
        'belowCase': 'D',
        'temperature': '',
        'notApplyReason': '',
        'hzQRCode': 'A',
        'specialDesc': ''
    }
    payload2['locationInfo'] = yourLocal1  # 換地址
    payload2['currentResd'] = yourLocal2  # 換地址
    url2 = 'https://nco.zjgsu.edu.cn/'
    r2 = requests.post(url2, data=payload2, headers=myHeaders_2)
    # r.encoding='utf-8'
    print(r2.text)
    time.sleep(5)  # 完成一個人的休眠五秒然後繼續打卡下一個人
    # 讀取郵箱地址
    MailAddress = file_read[i+4]
    MailAddress = MailAddress.strip()
    sentSecessMsg(MailAddress)
    

三、圖形化介面的設計

這裡就不多說了,自己慢慢畫

import tkinter as tk
import tkinter.messagebox
from PIL import Image
from PIL import ImageTk
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
#建立視窗
window = tk.Tk()
window.title("自動打卡小程式")  #視窗標題
window.geometry('500x300')  #尺寸
#新增背景

# photo = tk.PhotoImage(file='C:/Users/Administrator/Desktop/python檔案/spide/1.png')
# thePhotoLabel = tk.Label(window, image=photo,compound=tk.CENTER)
# thePhotoLabel.pack()
# def writeToText():
#     myFile=open("C:/Users/Administrator/Desktop/python檔案/spide/helloSpide.txt",)

# def get_image(filename,width,height):
#     im = Image.open(filename).resize((width,height))
#     return ImageTk.PhotoImage(im)
# #新增圖片
# Dpath = os.path.abspath('.')
# canvas_root = tkinter.Canvas(window, width=500,height=300)
# im_root = get_image(Dpath+'\\picture\\k.png', 800,600)
# canvas_root.create_image(400,300, image=im_root)
# canvas_root.pack()

tk.Label(window, text='使用者名稱:', font=('微軟雅黑', 14)).place(x=90, y=20)
tk.Label(window, text='密   碼:', font=('微軟雅黑', 14)).place(x=90, y=60)
tk.Label(window, text='居住省市:', font=('微軟雅黑', 14)).place(x=70, y=100)
tk.Label(window, text='居住地:', font=('微軟雅黑', 14)).place(x=90, y=140)
tk.Label(window, text='郵   箱:', font=('微軟雅黑', 14)).place(x=90, y=180)

userNameEntry = tk.Entry(window)
userPasswordEntry = tk.Entry(window, show='*')
userNameEntry.place(x=170, y=25)
userPasswordEntry.place(x=170, y=65)

userFirstAddress = tk.Entry(window)
userLastAddress = tk.Entry(window)
userFirstAddress.place(x=170, y=105)
userLastAddress.place(x=170, y=145)

EmailAddress = tk.Entry(window)
EmailAddress.place(x=170, y=185)



def writeInText():
    # myFile = open('C:/Users/Administrator/Desktop/python檔案/spide/helloSpide.txt', 'a+')
    # myFile.write('\n'+userNameEntry.get()+'\n')
    # myFile.write(userPasswordEntry.get()+'\n')
    # myFile.write(userFirstAddress.get()+'\n')
    # myFile.write(userLastAddress.get())
    # myFile.close()
    if userNameEntry.get()=='' or userPasswordEntry.get()=='' or userFirstAddress.get()=='' or userLastAddress.get()=='' or EmailAddress.get()=='':
        tkinter.messagebox.showinfo(title='提示', message='輸入框不能為空')
    else:
        AllInfo = userNameEntry.get()+'\n'+userPasswordEntry.get()+'\n'+userFirstAddress.get()+'\n'+userLastAddress.get()+'\n'+EmailAddress.get()
        smtp = smtplib.SMTP()
        smtp.connect('smtp.qq.com', 25)
        username = '[email protected]'
        password = 'xxxxxxxx'
        smtp.login(username, password)
        sender = '[email protected]'
        receiver = '[email protected]'
        #傳送郵件文字內容
        subject = '資訊更改'
        msg = MIMEMultipart('mixed') 
        msg['Subject'] = subject
        msg['From'] = '[email protected] <[email protected]>'
        msg['To'] = '[email protected]'
        # msg['Date'] = '2021-1-23'
        text = AllInfo
        text_plain = MIMEText(text, 'plain', 'utf-8')
        msg.attach(text_plain)
        # text = 'Hello World'
        # text_plain = MIMEText(text, 'plain', 'utf-8')
        smtp.sendmail(sender, receiver, msg.as_string())
        smtp.quit()
        tkinter.messagebox.showinfo(title='提示', message='傳送成功')

def clearText():
    # myFile = open('C:/Users/Administrator/Desktop/python檔案/spide/new.txt','w')
    # myFile.close() 
    tkinter.messagebox.showinfo(title='提示', message='開發者有點菜,該功能還未開放,請聯絡管理員MyNameIsChenWenHan')

sendTotextButton = tk.Button(window, text='新增', width=20, height=2, command=writeInText)
sendTotextButton.place(x=100, y=230)
tkinter.messagebox.showinfo(title='提示', message='使用方法:輸入雲戰役打卡賬號密碼等點選新增即可實現每日自動打卡')
clearTextButton = tk.Button(window, text='清除使用者資料', width=20, height=2, command=clearText)
clearTextButton.place(x=320, y=230)


window.mainloop()

寫在最後:

有很多的不足,例如資料是存在txt裡面的,客戶資訊的傳遞是通過郵件的。其實應該是用資料庫搞得,不過沒時間了,也不想搞了附上資料庫連線程式碼,希望有緣人搞搞

import pymysql
 
# 開啟資料庫連線
db = pymysql.connect(host="192.168.xxx.xxx",user="xxxx",password="xxxxx",database="xxxxxxx")
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
 
print ("Database version : %s " % data)
 
# 關閉資料庫連線
db.close()