通过爬虫和gui界面做成简易的exe文件,
目前已经弃用(测试版)
以下是gui界面设置,
win() 方法是居中+冻结窗体设置,单独列出提升可读性也方便组装(可以自己设置一些简易gui界面模版方便以后直接填充)
text() 设置单行输入框、可变数据(网址)和功能键
from tkinter import messagebox
import requests, re
from bs4 import BeautifulSoup
import tkinter
from tkinter.constants import CURRENT
# import sys
# import os
# 设置绝对路径
# def get_ico_src(relative_src):
# if getattr(sys, "frozen", False):
# base_path = sys._MEIPASS
# else:
# base_path = os.path.abspath(".")
# return os.path.join(base_path, relative_src)
# icon_src=get_ico_src(os.path.join("resources","versioncuecs2.ico"))
class MainForm():
def __init__(self):
# 设置一个页面
self.root = tkinter.Tk()
# 设置页面总标题
self.root.title('啦啦啦专属')
# 界面logo
self.root.iconbitmap(icon_src)
# 设置窗口
self.win()
# 设置背景颜色
self.root["background"] = "orange"
# 输入处理
self.text()
self.root.mainloop()
def win(self):
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
x = (self.screen_width - 500) / 2
y = (self.screen_height - 90) / 2
# 设置居中窗口
self.root.geometry("500x90+%d+%d" % (x, y))
# 禁止修改窗体
self.root.resizable(width=False,height=False)
def text(self):
self.content = tkinter.StringVar()
self.entry = tkinter.Entry(
self.root,
width=50,
textvariable=self.content,
font=(
'微软雅黑',
20))
self.button = tkinter.Button(
self.root, text='确定', fg="black", font=(
"微软雅黑", 12))
self.button.bind('<Button-1>', self.task)
self.entry.pack()
self.button.pack()
if __name__ == "__main__":
MainForm()
程序核心,
通过请求客户给予的商品界面网址,下载该商品的主视频和图片资料
设置多个报错是为了方便以后的维护
cookie的设置还可以进一步优化,比如会话(session)
# 主程序
def task(self, event):
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36",
"cookie": "保密,自己设"
}
search_url = self.entry.get()
res = requests.get(search_url, headers=headers)
soup = BeautifulSoup(res.text, "html.parser")
try:
data = soup.find("ul", class_="nav-tabs").find_all("li")
except:
messagebox.showerror("错误提示", "网址选择错误!!!")
self.root.protocol("WM_DELETE_WINDOW", self.root.destroy())
obj_01 = re.compile(r'"original":"(.*?)"}', re.S)
for photo_url in data:
# li标签 , class="tab-trigger"属性 , data-imgs文件位置
result_url = obj_01.findall(photo_url.get("data-imgs"))
# print(result_url[0])
file_01 = result_url[0].split("ibank/")[-1]
file_name = file_01.replace("/", "p")
# print(file_name)
try:
result_res = requests.get(result_url[0])
with open(file_name, mode="wb") as f:
f.write(result_res.content)
except:
messagebox.showerror("错误提示", "下载失败!!!")
self.root.protocol("WM_DELETE_WINDOW", self.root.destroy())
messagebox.showinfo("温馨提示", "下载完成!!!")
self.content.set("")
源码:
CSDN-/阿里1688资料.py at 8c65a1b0ee133bf98eaee1acd905910d8337f7fe · thinkloading/CSDN- · GitHub
鉴于当前普遍的图片和滑块验证码
可以通过selenium登陆获取cookie
再通过以下的转换公式去处理成requests可用的格式
感兴趣的可以自己去试试,这边不在展开 < 危(/▽\) >
# res爬虫kookie转给selenium
re = requests.get(url)
cookies = res.cookies.get_dict()
for k, v in cookies.items():
driver.add_cookie({"name": k, "value": v})
# selenium的cookie转res
for cookie in cookies_1:
cookies[cookie['name']] = cookie['value']