使用方法:
注意事项:
cookie 如果报错,就是这个cookie的问题,刷新页面,cookie是==结尾的。
下载数大概就是当前接口下再请求多少次,1次大约多30张左右。
代码如下:
from PySide2.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QLabel, QFormLayout, QProgressBar
import sys
import os
import jsonpath
import requests
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse
import time
def generate_next_url(current_url):
"""根据当前URL生成下一个URL"""
# 解析URL
parsed_url = urlparse(current_url)
query_params = parse_qs(parsed_url.query)
# 生成新的pn值(假设每次递增30)
current_pn = int(query_params.get('pn', [0])[0])
new_pn = current_pn + 30
# 将新的pn值转换为十六进制并格式化成小写
new_gsm = hex(new_pn)[2:].zfill(2) # 确保至少有两个字符
# 生成新的时间戳
new_timestamp = str(int(time.time() * 1000)) # 当前毫秒级时间戳
# 更新查询参数
query_params['pn'] = [str(new_pn)]
query_params['gsm'] = [new_gsm]
# 重新构建查询字符串(不包括时间戳)
new_query_string = urlencode(query_params, doseq=True)
# 重新构建完整的URL,添加时间戳到查询字符串
new_url = urlunparse(parsed_url._replace(query=new_query_string)) + f"&{new_timestamp}="
return new_url
def get_png(url, cookie):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
'Connection': 'keep-alive',
'Cookie': cookie
}
response = requests.get(url, headers=headers)
return jsonpath.jsonpath(response.json(), '$..thumbURL')
def download_images(url_list, save_directory='downloaded_images', progress_callback=None):
"""
从给定的URL列表下载图片,并将它们保存到指定目录中。
:param url_list: 包含图片URL的列表
:param save_directory: 图片保存的目录
:param progress_callback: 用于更新进度条的回调函数
"""
# 如果保存目录不存在,则创建目录
if not os.path.exists(save_directory):
os.makedirs(save_directory)
total = len(url_list)
counter = 1
for url in url_list:
try:
# 使用计数器生成文件名
image_name = f"{counter}.jpg" # 这里假设所有图片都是JPG格式,你可以根据实际情况更改扩展名
image_path = os.path.join(save_directory, image_name)
# 发送请求并获取响应内容
response = requests.get(url, stream=True)
response.raise_for_status() # 确保请求成功
# 将图片内容写入文件
with open(image_path, 'wb') as file:
file.write(response.content)
# 更新进度条
if progress_callback:
progress_callback(counter / total * 100)
# 递增计数器
counter += 1
except requests.RequestException as e:
print(f"下载失败 {url}: {e}")
except OSError as e:
print(f"文件保存失败 {url}: {e}")
def downloadpng(url, cookie, number, progress_callback):
listurl = []
urlss = []
listurl.append(url)
for i in range(int(number)-1):
next_url = generate_next_url(url)
listurl.append(next_url)
for urls in listurl:
res_list = get_png(urls, cookie)
for isss in res_list:
urlss.append(isss)
download_images(urlss, progress_callback=progress_callback)
class DownloadApp(QWidget):
def __init__(self):
super().__init__()
# 创建控件
self.url_input = QLineEdit()
self.cookie_input = QLineEdit()
self.download_count_input = QLineEdit()
self.start_button = QPushButton("开始下载")
self.progress_bar = QProgressBar()
# 设置布局
self.layout = QFormLayout()
self.layout.addRow(QLabel("URL:"), self.url_input)
self.layout.addRow(QLabel("Cookie:"), self.cookie_input)
self.layout.addRow(QLabel("下载数:"), self.download_count_input)
self.layout.addRow(self.start_button)
self.layout.addRow(QLabel("进度:"), self.progress_bar)
self.setLayout(self.layout)
# 连接信号和槽
self.start_button.clicked.connect(self.start_download)
def start_download(self):
url = self.url_input.text()
cookie = self.cookie_input.text()
download_count = self.download_count_input.text()
# 初始化进度条
self.progress_bar.setValue(0)
downloadpng(url, cookie, download_count, self.update_progress)
def update_progress(self, value):
"""更新进度条的值"""
self.progress_bar.setValue(value)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = DownloadApp()
window.setWindowTitle("下载照片")
window.resize(300, 150)
window.show()
sys.exit(app.exec_())