Tkinter和selenium结合实现登录UC后台,最后打包成exe

2024-06-24 09:08

本文主要是介绍Tkinter和selenium结合实现登录UC后台,最后打包成exe,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要实现的功能:小号模式自动登录UC阿里汇川广告后台,屏蔽账号密码输入
主要用的技术:用Tkinter展示所有的广告账号界面,使用selenium控制谷歌浏览器,打开阿里汇川登录页,登录汇川后台。
第一次写,遇到的坑比较多,三天,搞定。给自己一个棒棒~☺️

import Tkinter as tk  
import os
import sys
import requests
from selenium import webdriver
import base64
from PIL import Image, ImageTk
import mathpath = os.path.split(os.path.realpath(__file__))[0] + os.sepclass showCode(tk.Toplevel):def __init__(self):# super().__init__()tk.Toplevel.__init__(self)self.title('请输入验证码')self.wm_attributes('-topmost',1)# 弹窗界面self.setup_UI()def setup_UI(self):# from PIL import Image# im = Image.open(path + 'code/code.gif')# im.show()self._inputCode = tk.StringVar() # 将此处的验证码放置到类中,直接获取该值无法获取frame = tk.Frame(self)  #初始化一个框架frame.pack()im=Image.open(path  + "/code/code.gif")  self.photo=ImageTk.PhotoImage(im)  cv = tk.Canvas(frame)  cv.pack(side='top',fill='both', expand='false',padx=20,pady=20)  cv.create_image(0, 0, image=self.photo, anchor='nw')  # im=Image.open(path + 'code/code.gif')# img=ImageTk.PhotoImage(im)# imLabel=tk.Label(frame,image=img).pack()# 显示图片e1 = tk.Entry(frame,width=10,textvariable=self._inputCode).pack(anchor='w')tk.Button(frame,text='确定',command=self.setCode).pack(anchor='w')#点击确定将验证码赋值给主窗口  def setCode(self):self.inputCode = self._inputCode.get()self.destroy() # 销毁窗口# 主窗
class MyApp(tk.Tk):def __init__(self):global path# super().__init__()tk.Tk.__init__(self)#self.pack() # 若继承 tk.Frame ,此句必须有!self.title('UC账号管家')self.geometry('1000x600')self.iconbitmap(default='code/logo.ico')  # 加图标# 获取所有账号self.getAccountsFirst()    # 调起程序界面self.setupUI()# 获取所有的投放账号def getAccountsFirst(self):f = open(path + 'code/account.log')content = f.read()f.close()#解密账号_account = base64.b64decode(content).replace(' ', '').split("\n")__account = []for i in range(len(_account)):__account.append(_account[i].split(","))# self.account[i] = _account[i].split(",")#将账号赋值给账号界面self.accounts = __account#拉起账号界面def setupUI(self):def callback():argAccount = var.get()for account in self.accounts:if(account[0].decode('utf-8') == argAccount):# 此时做登录操作self.login(account[0],account[1])row1 = tk.LabelFrame(self)var = tk.StringVar()group=tk.LabelFrame(row1,text='选择要登录的账号').pack(padx=100)   # 基于root 制定一个框架 . count = 0row = 1for name,pwd in self.accounts:column = int(math.fmod(count,5))if(row * 5 == count):row = row + 1b = tk.Radiobutton(group,text=name,variable=var,value=name,indicatoron=0,padx=30,pady=3,command=callback)b.grid(row=row,column=column)count = count + 1# b.pack(anchor="w")label = tk.Label(row1)label.pack()def login(self,username,pwd):#开始登陆login_url = 'https://e.uc.cn/'headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) ''Chrome/58.0.3029.110 Safari/537.36'}options = webdriver.ChromeOptions()options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])options.add_argument('disable-infobars')driver = webdriver.Chrome(chrome_options=options)# options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"# driver.chrome_driver_binary = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"# driver = webdriver.Chrome()driver.delete_all_cookies()driver.get("https://e.uc.cn/sso/static/pages/hcsso.html?authorizeUrl=http%3A%2F%2Fe.uc.cn%2Fsso%2Fv1%2Fauthorize%3Fresponse_type%3Dcode%26scope%3Dopenid%26client_id%3DeyJjdHkiOiJKV1QiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiQTEyOEtXIn0.PYe1Zfthswo5iITZVGBkn5hCfdzyuQAkXWPE7VAO0eAuRNEzikoQgQ.3g8Cf2JZ2D8bhKdKlgaHGw.-56UrnfHvZPoryCii7MPSvoBUlnacryxYxBqQBLUQe3k_XJ1j3Trv-Io9JK9Pg3CyFbslM281pe8MsNGjvIfPUT7Z_Vc0N_0t_bCp3Lp4H0eJ3YODXapXgMX2qYROw1uWCbb8FcWpa37HyySgjsaOHIiDVkwmb3I_flywaJu5Tavbp09nwirOg-AJ8unie4nQXt9XqA5Dpq9F5qv36TFGVKRUbTMzvA8XGjUpE1ExL5oodpxfh8FfUfBLvF69PgjHwJiijrC702X5VHx0Og949xnmIX4znCbJ3MPUDjPwtS92IxGYypjWDTi4bfWYZYl8iPVoMbZCCz--QrmH1FUjznu6YmVavmKXasvHg6qI9Gpn5hnv--9rPp0CJgpJg_7DLyxr4D7jOj2Ju7gXGNPKqsoqP_GT1zsa1mkUZbeMSLYQVZJPwO6HLcpyZG-zH1_fz70IgIxACHBxky2V3P9WgUaExKys-e_8zawHxWpbyTl1DZlg7v1razzzrwgteHmpoJxyRUt_Gs7WQyYlCrgLrhVWEF7JKTy9MJFeepuGg8GWyzUtu86yRkIkDDL726B4J_sa779JzfutyZtVBAj36BuSj_EnvFsNau13vJ23Nyc3wm8TT8QS6xD6vrKRHWYCp7trDHcInlIczi7gnVth2c_RstjKeE8wcIPN1a1-4wkwIiUAkrnx_y2EaUECHp5vHITjbpBD1rPzZwKZT8Mlw.9hgjcRYoM3FgvXVF7YOgIw%26redirect_uri%3Dhttps%3A%2F%2Fe.uc.cn%2Fuc%2Fsso_auth_code%26target_url%3Dhttp%3A%2F%2Fe.uc.cn%2Fuc%2F%3FisFromPortal%3D0")# 将所有的cookie清除driver.delete_all_cookies()#重新获取验证码并且将cookie写入浏览器cookies = self.getCode()for cok in cookies:driver.add_cookie({'name':cok,'value':cookies[cok]})# 弹窗code = self.ask_inputCode()userName = driver.find_element_by_id('username')password = driver.find_element_by_id('password')captchaCode = driver.find_element_by_name('captchaCode')userName.clear()userName.send_keys(username.decode('utf-8'))password.clear()password.send_keys(pwd.decode('utf-8'))captchaCode.send_keys(code.decode('utf-8'))submit = driver.find_element_by_class_name('js-hc-loginBtn')submit.click()def ask_inputCode(self):inputOjb = showCode()self.wait_window(inputOjb) # 这一句很重要!!!return inputOjb.inputCodedef getCode(self):global pathimg_src = "https://e.uc.cn/sso/auth.jpg"r = requests.get(img_src)r.raise_for_status()with open(path + 'code/code.gif',"wb") as f: #开始写文件,wb代表写二进制文件f.write(r.content)f.close()f.close()cookies = requests.utils.dict_from_cookiejar(r.cookies)return cookiesif __name__ == '__main__':app = MyApp()app.mainloop()

关于环境:

我是在mac里面先写完,然后最后因为要编译成exe,所以必须在window里面重新再搭建环境。
遇到的问题:
问题一、
同样是使用Tkinter,环境py27
mac中 import tkinter as tk 是没问题的

在window中使用    import tkinter as tk  报 ImportError:No module named tkinter

解决办法:原来一直以为是没有装tkinter,因为在mac里面使用(
mac安装Tkinter库brew install homebrew/dupes/tcl-tk)重新过tkinter,以为window也要重新搞一遍
后来发现用法用错了。在window中py2.7 要使用

import Tkinter as tk

问题二、

关于代码

问题一、最开始没有使用类的方式,所有的方法都放在一个文件中,然后在弹窗将输入的值返回给主界面,一直返回不回去。如果要设置的参数个数超过两个,那么tkinter提供的标准窗口就处理不了了。怎样将自定义窗口中的数据传回主窗口?
解决办法:原来一直以为是对python的类理解不深刻,然后是Tkinter中的使用就是返回不回去
参照:https://www.jb51.net/article/119817.htm 修改成了类的方式来做。我用的第一种,把值返回给主界面

1)松耦合
说明:
主窗类,继承了 tk.Tk
弹窗类,继承了 tk.Toplevel
要点:
弹窗,将多个数据,打包,放入一个名为 username 的私有 list 对象,销毁弹窗
主窗,待弹窗运行后,通过wait_window方法,取得弹窗的名为 username 私有变量import tkinter as tk
'''松耦合'''
# 弹窗
class MyDialog(tk.Toplevel):def __init__(self):super().__init__()self.title('设置用户信息')# 弹窗界面self.setup_UI()def setup_UI(self):# 第一行(两列)row1 = tk.Frame(self)row1.pack(fill="x")tk.Label(row1, text='姓名:', width=8).pack(side=tk.LEFT)self.name = tk.StringVar()tk.Entry(row1, textvariable=self.name, width=20).pack(side=tk.LEFT)# 第二行row2 = tk.Frame(self)row2.pack(fill="x", ipadx=1, ipady=1)tk.Label(row2, text='年龄:', width=8).pack(side=tk.LEFT)self.age = tk.IntVar()tk.Entry(row2, textvariable=self.age, width=20).pack(side=tk.LEFT)# 第三行row3 = tk.Frame(self)row3.pack(fill="x")tk.Button(row3, text="取消", command=self.cancel).pack(side=tk.RIGHT)tk.Button(row3, text="确定", command=self.ok).pack(side=tk.RIGHT)def ok(self):self.userinfo = [self.name.get(), self.age.get()] # 设置数据self.destroy() # 销毁窗口def cancel(self):self.userinfo = None # 空!self.destroy()
# 主窗
class MyApp(tk.Tk):def __init__(self):super().__init__()#self.pack() # 若继承 tk.Frame ,此句必须有!self.title('用户信息')# 程序参数/数据self.name = '张三'self.age = 30# 程序界面self.setupUI()def setupUI(self):# 第一行(两列)row1 = tk.Frame(self)row1.pack(fill="x")tk.Label(row1, text='姓名:', width=8).pack(side=tk.LEFT)self.l1 = tk.Label(row1, text=self.name, width=20)self.l1.pack(side=tk.LEFT)# 第二行row2 = tk.Frame(self)row2.pack(fill="x")tk.Label(row2, text='年龄:', width=8).pack(side=tk.LEFT)self.l2 = tk.Label(row2, text=self.age, width=20)self.l2.pack(side=tk.LEFT)# 第三行row3 = tk.Frame(self)row3.pack(fill="x")tk.Button(row3, text="设置", command=self.setup_config).pack(side=tk.RIGHT)# 设置参数def setup_config(self):# 接收弹窗的数据res = self.ask_userinfo()#print(res)if res is None: return# 更改参数self.name, self.age = res# 更新界面self.l1.config(text=self.name)self.l2.config(text=self.age)# 弹窗def ask_userinfo(self):inputDialog = MyDialog()self.wait_window(inputDialog) # 这一句很重要!!!return inputDialog.userinfo
if __name__ == '__main__':app = MyApp()app.mainloop()2)紧耦合
说明:
主窗类,继承了 tk.Tk
弹窗类,继承了 tk.Toplevel
要点:
弹窗,显式地保存父窗口,显式地修改父窗口数据,显式地更新父窗口部件,最后销毁弹窗
主窗,待弹窗运行后,通过wait_window方法,返回 Noneimport tkinter as tk
'''紧耦合'''
# 弹窗
class PopupDialog(tk.Toplevel):def __init__(self, parent):super().__init__()self.title('设置用户信息')self.parent = parent # 显式地保留父窗口# 第一行(两列)row1 = tk.Frame(self)row1.pack(fill="x")tk.Label(row1, text='姓名:', width=8).pack(side=tk.LEFT)self.name = tk.StringVar()tk.Entry(row1, textvariable=self.name, width=20).pack(side=tk.LEFT)# 第二行row2 = tk.Frame(self)row2.pack(fill="x", ipadx=1, ipady=1)tk.Label(row2, text='年龄:', width=8).pack(side=tk.LEFT)self.age = tk.IntVar()tk.Entry(row2, textvariable=self.age, width=20).pack(side=tk.LEFT)# 第三行row3 = tk.Frame(self)row3.pack(fill="x")tk.Button(row3, text="取消", command=self.cancel).pack(side=tk.RIGHT)tk.Button(row3, text="确定", command=self.ok).pack(side=tk.RIGHT)def ok(self):# 显式地更改父窗口参数self.parent.name = self.name.get()self.parent.age = self.age.get()# 显式地更新父窗口界面self.parent.l1.config(text=self.parent.name)self.parent.l2.config(text=self.parent.age)self.destroy() # 销毁窗口def cancel(self):self.destroy()
# 主窗
class MyApp(tk.Tk):def __init__(self):super().__init__()# self.pack() # 若继承 tk.Frame,此句必须有!!!self.title('用户信息')# 程序参数self.name = '张三'self.age = 30# 程序界面self.setupUI()def setupUI(self):# 第一行(两列)row1 = tk.Frame(self)row1.pack(fill="x")tk.Label(row1, text='姓名:', width=8).pack(side=tk.LEFT)self.l1 = tk.Label(row1, text=self.name, width=20)self.l1.pack(side=tk.LEFT)# 第二行row2 = tk.Frame(self)row2.pack(fill="x")tk.Label(row2, text='年龄:', width=8).pack(side=tk.LEFT)self.l2 = tk.Label(row2, text=self.age, width=20)self.l2.pack(side=tk.LEFT)# 第三行row3 = tk.Frame(self)row3.pack(fill="x")tk.Button(row3, text="设置", command=self.setup_config).pack(side=tk.RIGHT)# 设置参数def setup_config(self):pw = PopupDialog(self)self.wait_window(pw) # 这一句很重要!!!return
if __name__ == '__main__':app = MyApp()app.mainloop()

问题二、
“Use of super on an old style class” py27环境里面super不支持使用,需要使用老方式来显示
Python类的继承有两种写法,一是老样式(old style),一是新样式(new style),都能解决上述的错误。
解决办法:
参照我的代码。
好的博客:这个涉及到关于py的类和继承,这个有时间再看下,因为直接做项目,蒙头做,回过头再研究吧
https://blog.csdn.net/ws_cs_dn/article/details/37699195
https://blog.csdn.net/yiifaa/article/details/78068970

问题三、
下载验证码到本地,然后使用Tkinter,打开该文件一直打不开。不显示该图片。最开始使用Lab方式来显示图片,反正死活没显示出来,后来使用tk.Canvas 然后显示出来图片……
解释:首先,查了下Tkinter的Lab只支持gif格式的图片显示,如果想支持多格式,需要下PIL包,然后用PIL的TkinterImage来打开图片(参照我代码),最开始我理解的,可能我下载的图片格式不对,我直接把验证码文件下载下来在保存的时候,直接给了后缀,可能文件格式有问题~这是一个
另一个是Lab的用法,可能在类里面的使用方式不对。

另外,我使用 tk.Canvas显示图片,也要格外注意self的使用,不然可能会出现弹窗和主界面主键混乱的情况。。。虽然我实现了功能,很多我也要重新再看下,我只给出解决的方式,至于为什么…………以后慢慢研究吧……

问题四、
在使用chromeDriver时,chromeDriver会有一个弹窗,如何隐藏chromeDriver.exe 黑色弹窗?
修改selenium包文件中
参照:https://stackoverflow.com/questions/33983860/hide-chromedriver-console-in-python
*\selenium\webdriver\common\services.py文件
首先引入 from win32process import CREATE_NO_WINDOW
然后修改start 方法(如下例子),增加creationflags=CREATE_NO_WINDOW

def start(self):"""Starts the Service.:Exceptions:- WebDriverException : Raised either when it can't start the serviceor when it can't connect to the service"""try:cmd = [self.path]cmd.extend(self.command_line_args())self.process = subprocess.Popen(cmd, env=self.env,close_fds=platform.system() != 'Windows',stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)except TypeError:raise

这样子,就不会显示黑色chromeDriver.exe 窗口啦,嘻嘻嘻嘻~

屏蔽,运行自动化脚本
option = webdriver.ChromeOptions()
option.add_argument(‘disable-infobars’)
webdriver.Chrome(chrome_options = option,desired_capabilities = None)

关于打包:
python
打包使用了pyinstaller 来打包。
pip install pyinstaller
pyinstaller -F uc-end.py –noconsole

这篇关于Tkinter和selenium结合实现登录UC后台,最后打包成exe的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089717

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too