一:本文目的
经常收到小伙伴需求,想在办公电脑上双击个exe文件,就能爬图片,上报ip,上传文件到git上 ;刚好一直在学习python,就想到了是否可以把pyhton脚本打包成exe,于是一顿百度,发现还真可以 ;而且牛皮的是:不需要安装pyhton运行环境。就是说,用python脚本打包的exe程序,发给小伙伴可以直接使用。 如果想省事,在加入到计划任务里,那就彻底解放了。 每天上班来了,就有爬图可以看,岂不美哉。
二:结果展示
三:过程概述
1.编写爬图脚本
2.打包为exe
3.双击执行
4.加入计划任务(略)
四:过程细节
1.编写爬图脚本
import requests
import json
def get_image_url():
url = 'https://image.baidu.com/search/index?tn=resulttagjson&logid=10358070151245603719&ie=utf-8&fr=&word=%E4%BD%9F%E4%B8%BD%E5%A8%85&ipn=r&fm=index&pos=history&queryWord=%E7%BE%8E%E5%A5%B3%E5%9B%BE%E7%89%87&cl=2&lm=-1&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=1&latest=©right=&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&expermode=&nojc=&isAsync=true&pn=0&rn=30&gsm=5a&1713426180895='
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
}
response = requests.get(url, headers=headers)
download_image = dict()
if response.status_code == 200:
data = response.text
json_data = json.loads(data)['data']
for image in json_data:
if image:
download_image[image['fromPageTitle']] = image['thumbURL']
return download_image
def download(image,title):
response = requests.get(image)
with open('image/' + title + '.jpg', mode="bw") as f:
f.write(response.content)
if __name__ == '__main__':
download_image = get_image_url()
for title, image in download_image.items():
title = title.replace('"','').replace('|','').replace('/','')
download(image, title)
2.打包为exe
pip3 install pyinstaller
pyinstaller -F -w pachong_tupian.py
3.双加执行exe
4.打开image文件夹查看图片
完!20240719 cupli