AutoInstall

分析

构思

  • 语言:Python
  • 预期
    • GUI,自己选择需要下载和安装的软件
    • 打开软件,弹出GUI,自己对软件进行选择,Next,弹出下载进度条;
    • 下载完成后,自动安装。
  • 库:
    • requests爬虫,爬取软件的安装包的下载地址
    • wget:下载软件的安装包
    • pywinauto:自动化打开软件安装包,并进行安装。
  • 难点:
    • 不会win32编程,不知道如何让安装过程自动化。
    • wget下载有点慢。
    • 生成的软件怎么获取?解决方法:
      • 上传到阿里云OSS,并把下载链接放在博客网站上,电脑开机后访问博客网站,然后下载。但这样的问题是,这个打包生成的exe文件太大了,网络流量太大了,会产生好大一笔费用,如果每天都下载一次的话,估计月底阿里云的余额就空了。
      • 依赖第三方服务,如:AirPortal,但这个的问题是,文件只能暂存,且下载次数有限。
      • 百度网盘获其他网盘:麻烦,下载链接记不住。
      • U盘,其实也是一个选择,但不够优雅!
      • ftp服务,我觉得这个不错,如果我写的好的话,所有人都觉得不错,想用的话,能不能申请放到学校的ftp服务器上,这样就是一个非常不错的解决方案啊!可是我写的不好,还有好多问题。

进度

  • 2022.11.11——v1.0&v2.0
    • 实现了QQ和微信的安装包自动下载;
    • 爬虫请求头轮转池;
    • 实现了QQ的自动化安装。

v2.0 autoDownload&Install

main.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding : utf-8 -*-
# Author : MurphyHou
# =======Here We Go!=======
import requests
import wget

from pywinauto import application
import time
import os
import random

from Install import installQQ
from Install import installWechat

def get_ua():
user_agents = [
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60',
'Opera/8.0 (Windows NT 5.1; U; en)',
'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 ',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; SE 2.X MetaSr 1.0) ',
]
user_agent = random.choice(user_agents) # 随机抽取对象
return user_agent

def downloadQQ():
## QQ
urlQQ = 'https://im.qq.com/pcqq'
ua = get_ua()
headers = {'User-Agent': ua}
html = requests.get(urlQQ, headers = headers)
websourceQQ = html.text # <class 'str'>

# <a href="https://dldir1.qq.com/qqfile/qq/PCQQ9.6.8/QQ9.6.8.28823.exe" class="download"></a>

indexStart = websourceQQ.find('https://dldir1.qq.com/qqfile/qq')
indexEnd = websourceQQ.find('class="download"')
indexEnd = indexEnd - 2
# print(websourceQQ[indexStart:indexEnd])
qqDownloadUrl = websourceQQ[indexStart:indexEnd]
# print(qqDownloadUrl)
return qqDownloadUrl

def downloatWechat():
## Wechat
urlWechat = 'https://pc.weixin.qq.com/'
ua = get_ua()
headers = {'User-Agent': ua}
html = requests.get(urlWechat, headers = headers)
websourceUrlWechat = html.text # <class 'str'>

# <a class="download-button" id="downloadButton" href="https://dldir1.qq.com/weixin/Windows/WeChatSetup.exe"><i class="download-icon"></i><div class="download-info"><div>立即下载</div><span class="download-version">3.8.0</span></div></a>

indexStart = websourceUrlWechat.find('https://dldir1.qq.com/weixin/Windows')
indexEnd = websourceUrlWechat.find('<i class="download-icon">')
indexEnd = indexEnd - 2
global wechatDownloadUrl
wechatDownloadUrl = websourceUrlWechat[indexStart:indexEnd]
# print(wechatDownloadUrl)
return wechatDownloadUrl

#### wget.download(url, out=None, bar=bar_adaptive)

if __name__ == '__main__':
fileList = os.listdir()

flag = 0
for i in fileList:
if i == 'installer':
flag = 1
break
if flag == 0:
os.mkdir('installer')

basePath = os.getcwd() + '\\'
downloadList = []
downloadList.append(downloadQQ()) # index = 0
downloadList.append(downloatWechat()) # index = 1
targetName = ['0QQ.exe', '1Wechat.exe']

for i in range(len(downloadList)):
wget.download(downloadList[i], out = basePath + 'installer\\' + targetName[i])
# print(downloadList[i])

installQQ(basePath + 'installer\\' + targetName[0])
installWechat(basePath + 'installer\\' + targetName[1])

Install.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding : utf-8 -*-
# Author : MurphyHou
# =======Here We Go!=======

from pywinauto import application
import time

def installQQ(path):
app = application.Application().start(path)
time.sleep(2)
window_title='腾讯QQ安装向导'
app[window_title].child_window(title="阅读并同意").click()
app[window_title].child_window(class_name="#32770").child_window(title="立即安装", class_name="Button").click()
# time.sleep(30) # 啊?这个时间好像不是很好把控
# app[window_title].child_window(class_name="#32770").child_window(title="完成安装", class_name="Button").click()

def installWechat(path):
app = application.Application().start(path)
time.sleep(15)# 啊?这个时间好像不是很好把控
window_title='微信安装向导'

# 这里有问题,不知道如何定位
# app[window_title].child_window(title="我已阅读并同意服务协议和隐私协议").click()
# app[window_title].child_window(title="安装", class_name="Button").click()

参考资料

python模块 - pywinauto(windows自动化安装软件) - Anec - 博客园 (cnblogs.com)

python基于pywinauto实现PC端自动化 python操作微信自动化 - www.pu - 博客园 (cnblogs.com)

爬虫请求头headers-User-Agent轮转池_等会吃个橘子的博客-CSDN博客_请求头池

GitHub - blackrosezy/gui-inspect-tool: Gui Inspect tool for Windows


AutoInstall
https://cosmicdusty.cc/post/Ideas/AutoInstall/
作者
Murphy
发布于
2022年11月11日
许可协议