声明:本程序基于Python3.11解释器制作
会有闪退的现象出现不清楚是为什么,有能力请自行修改
(不一定100%会有,但是有概率出现)
import os, re, requests, subprocess, sys, threading, webbrowser, time, random
from urllib.parse import urljoin, urlparse
from PyQt5 import QtWidgets, QtCore
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
def check_and_install_packages():
for package in ['requests', 'PyQt5']:
try:
__import__(package)
except ImportError:
install(package)
def log_message(message):
log_text.append(message)
def get_website_source(url):
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
log_message(f"成功获取网站源代码: {url}")
return response.text
except requests.RequestException as e:
log_message(f"无法访问网址: {url}. 错误: {e}")
return None
def extract_resources(html_content, base_url):
links = re.findall(r'<link[^>]+href="([^"]+)"|<script[^>]+src="([^"]+)"|<img[^>]+src="([^"]+)"|<a[^>]+href="([^"]+\.html?)"', html_content)
return {urljoin(base_url, link) for group in links for link in group if link}
def download_resource(url, folder, delay):
try:
time.sleep(delay)
response = requests.get(url, stream=True, timeout=10)
response.raise_for_status()
filename = os.path.join(folder, os.path.basename(urlparse(url).path) or 'index.html')
with open(filename, 'wb') as file:
for data in response.iter_content(8192):
file.write(data)
log_message(f"下载完成: {url}")
except Exception as e:
log_message(f"下载失败: {url}. 错误: {e}")
def process_resources(url, folder, delay):
html_content = get_website_source(url)
if html_content:
resources = extract_resources(html_content, url)
log_message(f"找到 {len(resources)} 个资源,开始下载...")
for resource in resources:
threading.Thread(target=download_resource, args=(resource, folder, delay)).start()
def start_download():
url, folder = url_entry.text(), path_entry.text()
if url and folder:
delay = 1 if high_frequency_var.isChecked() and QtWidgets.QMessageBox.question(window, "警告", "您选择了高频访问,可能会被防火墙拦截。是否继续?") == QtWidgets.QMessageBox.Yes else random.randint(10, 20)
log_message("开始下载...")
process_resources(url, folder, delay)
log_message("下载任务已提交,请等待完成。")
def browse_folder():
folder_selected = QtWidgets.QFileDialog.getExistingDirectory(window)
if folder_selected:
path_entry.setText(folder_selected)
def exit_program():
QtWidgets.qApp.quit()
def open_contact_link():
webbrowser.open("mailto:[email protected]")
def show_warning_message():
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Warning)
msg.setText("本工具仅供学习,禁止用于违法用途,禁止将爬取的网站资源文件私自使用,一切版权归属于原网站")
msg.setWindowTitle("警告")
msg.setStyleSheet("QLabel { color : red; }")
msg.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
msg.setWindowModality(QtCore.Qt.ApplicationModal)
msg.setWindowFlags(msg.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
return msg.exec_()
check_and_install_packages()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("网站源码下载工具 v1.0测试版")
window.setFixedSize(600, 600)
window.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
if show_warning_message() == QtWidgets.QMessageBox.Cancel:
exit_program()
layout = QtWidgets.QVBoxLayout()
layout.addWidget(QtWidgets.QLabel("请输入网址(请使用快捷方式粘贴):"))
url_entry = QtWidgets.QLineEdit()
url_entry.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
layout.addWidget(url_entry)
layout.addWidget(QtWidgets.QLabel("请选择保存路径:"))
path_entry = QtWidgets.QLineEdit()
path_entry.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
layout.addWidget(path_entry)
button_layout = QtWidgets.QHBoxLayout()
button_layout.addWidget(QtWidgets.QPushButton("浏览", clicked=browse_folder))
button_layout.addWidget(QtWidgets.QPushButton("下载资源", clicked=start_download))
button_layout.addWidget(QtWidgets.QPushButton("退出", clicked=exit_program))
layout.addLayout(button_layout)
layout.addWidget(QtWidgets.QPushButton("联系我", clicked=open_contact_link))
high_frequency_var = QtWidgets.QCheckBox("使用高频访问(可能会被防火墙拦截)")
layout.addWidget(high_frequency_var)
log_text = QtWidgets.QTextEdit()
log_text.setReadOnly(True)
log_text.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
layout.addWidget(log_text)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
需要安装运行库,否则直接报错,解释器随便都可以
有问题请联系我,在本平台上不要通过分享代码中的联系方式,我并不会回复
标签:layout,log,Python,QtWidgets,url,源码,folder,message,下载 From: https://blog.csdn.net/2402_87876495/article/details/143450220