前情提要
最近发现一款特别好看的壁纸软件,其中提供了鼠标样式,感觉很好看!很精致!心想肯定是请求下载然后启用鼠标样式, 那么发送请求,那不就可以用Python爬虫了吗?
其中鼠标样式下图:
爬虫环境
抓包软件: Fiddler4
编写软件: VS Code
抓包分析请求
这里我抓包用的是Fiddler4,先点击×号清空杂乱的数据:
然后我们打开软件,随便点击一个鼠标样式点击下载,这个时候我们能看到抓包软件有请求出现,点击右边的Inspectors,下方的JSON,诶诶?这是什么?
我们注意下圆圈中是 desk_mouseList ,再结合下方返回的JSON一看,这!就是我们想要的地址呀。我们把JSON拿到JSON格式化网站一看,
隐隐感觉url就是我们要找的东西!复制到浏览器一看,一个压缩包!打开一看,里面就是鼠标样式文件!
你以为就这样就分析完了吗?不不不,远不于此。
这里补给一波常识,一般一个鼠标样式安装文件夹中,必须有一个AutoSetup.inf文件来进行安装文件。
但是!但是!但是!这里没有呀,那我们怎么办?
我这里找来了一个AutoSetup.inf文件的内容:
[Version]
signature="$CHICAGO$"
[DefaultInstall]
CopyFiles = Scheme.Cur
AddReg = Scheme.Reg,Wreg
[DestinationDirs]
Scheme.Cur = 10,"%CUR_DIR%"
[Scheme.Reg]
HKCU,"Control Panel\Cursors\Schemes","%SCHEME_NAME%",,"%10%\%CUR_DIR%\%pointer%,%10%\%CUR_DIR%\%help%,%10%\%CUR_DIR%\%work%,%10%\%CUR_DIR%\%busy%,%10%\%CUR_DIR%\%Cross%,%10%\%CUR_DIR%\%Text%,%10%\%CUR_DIR%\%Hand%,%10%\%CUR_DIR%\%Unavailiable%,%10%\%CUR_DIR%\%Vert%,%10%\%CUR_DIR%\%Horz%,%10%\%CUR_DIR%\%Dgn1%,%10%\%CUR_DIR%\%Dgn2%,%10%\%CUR_DIR%\%move%,%10%\%CUR_DIR%\%alternate%,%10%\%CUR_DIR%\%link%"
[Wreg]
HKCU,"Control Panel\Cursors",,0x00020000,"%SCHEME_NAME%"
HKCU,"Control Panel\Cursors",AppStarting,0x00020000,"%10%\%CUR_DIR%\%work%"
HKCU,"Control Panel\Cursors",Arrow,0x00020000,"%10%\%CUR_DIR%\%pointer%"
HKCU,"Control Panel\Cursors",Crosshair,0x00020000,"%10%\%CUR_DIR%\%Cross%"
HKCU,"Control Panel\Cursors",Hand,0x00020000,"%10%\%CUR_DIR%\%link%"
HKCU,"Control Panel\Cursors",Help,0x00020000,"%10%\%CUR_DIR%\%Help%"
HKCU,"Control Panel\Cursors",IBeam,0x00020000,"%10%\%CUR_DIR%\%Text%"
HKCU,"Control Panel\Cursors",No,0x00020000,"%10%\%CUR_DIR%\%Unavailiable%"
HKCU,"Control Panel\Cursors",NWPen,0x00020000,"%10%\%CUR_DIR%\%Hand%"
HKCU,"Control Panel\Cursors",SizeAll,0x00020000,"%10%\%CUR_DIR%\%move%"
HKCU,"Control Panel\Cursors",SizeNESW,0x00020000,"%10%\%CUR_DIR%\%Dgn2%"
HKCU,"Control Panel\Cursors",SizeNS,0x00020000,"%10%\%CUR_DIR%\%Vert%"
HKCU,"Control Panel\Cursors",SizeNWSE,0x00020000,"%10%\%CUR_DIR%\%Dgn1%"
HKCU,"Control Panel\Cursors",SizeWE,0x00020000,"%10%\%CUR_DIR%\%Horz%"
HKCU,"Control Panel\Cursors",UpArrow,0x00020000,"%10%\%CUR_DIR%\%alternate%"
HKCU,"Control Panel\Cursors",Wait,0x00020000,"%10%\%CUR_DIR%\%busy%"
HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Runonce\Setup\","",,"rundll32.exe shell32.dll,Control_RunDLL main.cpl @0"
[Scheme.Cur]
"pointer.ani"
"help.cur"
"work.ani"
"busy.ani"
"cross.cur"
"text.cur"
"hand.cur"
"unavailiable.cur"
"vert.cur"
"horz.cur"
"dgn1.cur"
"dgn2.cur"
"move.cur"
"alternate.cur"
"link.cur"
[Strings]
CUR_DIR = "Cursors\卡通简笔"
SCHEME_NAME = "卡通简笔"
pointer= "pointer.ani"
help= "help.cur"
work= "work.ani"
busy= "busy.ani"
cross= "cross.cur"
text= "text.cur"
hand= "hand.cur"
unavailiable= "unavailiable.cur"
vert= "vert.cur"
horz= "horz.cur"
dgn1= "dgn1.cur"
dgn2= "dgn2.cur"
move= "move.cur"
alternate= "alternate.cur"
link= "link.cur"
不卖关子啦,我花费了近半小时来找规律,就是最后的CUR_DIR和SCHEME_NAME的值不一样啦!改成鼠标样式的名字叫就行了!
那我们,开干代码!
编写代码:
首先我们请求第一个API接口地址,以此来获取所有的鼠标样式信息列表:
# 获取所有光标下载列表
def getMouseList():
url = "https://pcwallpaper.zhhainiao.com/config/get/by/ckey?ckey=desk_mouseList"
header = {
"Host": "pcwallpaper.zhhainiao.com",
"Connection": "keep-alive",
"Accept": "application/json, text/plain, */*",
"Origin": "https://wallpaper.zhhainiao.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
"Content-Type": "application/json;charset=utf-8",
"Referer": "https://wallpaper.zhhainiao.com/3012/beautifyMouse?"
"open_id=1327620721665380352&client=1&os=12&protocol=11",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN",
"keep_alive":"Flase"
}
r = requests.get(url=url, headers=header)
data = json.loads(r.text)["data"].replace('\n', '') # 提取响应json中的光标信息并替换所有换行
mouseList = json.loads(data)
return mouseList
然后我们开始遍历这个列表进行下载!:
# 下载所有光标皮肤
def downZip(mouseList):
if not os.path.exists(path):
os.makedirs(path)
# 遍历光标包名称及下载地址
for dic in mouseList:
print(f'正在下载:{dic["title"]},地址为: {dic["url"]}')
with open(path+dic["title"]+".zip","wb") as f:
z = requests.get(dic["url"]).content
f.write(z)
那么又一个问题来了,下载下来的是压缩包,我们要想创建AutoSetup.inf文件,必须在文件夹中创建啊?看来我们只能用Python来解压zip了!
# 解压所有压缩包
def unZipFile(mouseList):
# 解压所有安装包
for dic in mouseList:
zip_file = zipfile.ZipFile(path+dic["title"]+".zip")
zip_file.extractall(path+dic["title"]+"/")
createAutoSetup(path+dic["title"]+"/",dic["title"]) # 生成安装文件
print(f'正在解压{dic["title"]+".zip"}并生成安装文件...')
最后,我们为每一个文件夹创建AutoSetup.inf文件!
# 生成安装文件
def createAutoSetup(allPath,title):
f = open(allPath+"AutoSetup.inf","w")
fp.seek(0)
fe.seek(0)
f.writelines(fp.readlines())
f.write(f'CUR_DIR = "Cursors\{title}"\nSCHEME_NAME = "{title}"\n')
f.writelines(fe.readlines())
return
完整代码:
大小仅3KB,莫担心~
展示效果
愉快的使用这些漂亮的鼠标样式吧!
结束语
细心想想,生活中许多重复,繁琐的事情都可以用Python来完成。本次代码中实际到许多知识,抓包和分析,requests库的使用,zipfile库的使用,想练习Python的小伙伴可以试试哦~