某珍宝馆图片下载,自动合并
本帖最后由 hebeijianke 于 2024-7-26 21:29 编辑看到求助帖有人需要 @幸甚至哉
代码比较简单,只能下载较小的图片,图像的宽度和高度小于65500像素,好像python的几个图像处理模块都对拼接大图有限制,
注意:只能下载【书画】和【拾英】板块免费的,需要会员的不能下载,单线程下载,有点慢。
希望大佬优化一下代码吧。
import requests
from PIL import Image
from io import BytesIO
import os
def main(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Origin': 'https://g2.ltfc.net'}
tourToken = requests.post("https://api.quanku.art/cag2.TouristService/getAccessToken", headers=headers).json()['token']
# url = 'https://g2.ltfc.net/view/SUHA/657e79d2ace3dd0d7a7a74b4'
id1 = url.split('/')[-1]
T = url.split('/')[-2].lower().title()
u1 = f'https://api.quanku.art/cag2.{T}Service/get'
u2 = 'https://api.quanku.art/cag2.HDPicService/get'
u3 = 'https://api.quanku.art/cag2.HDPicService/getHDPicOfColl'
payload1 = {"Id": id1, "context": {"tourToken": tourToken, "appKey": "CAGWEB", "appSec": "ZETYK0B8KTQB41KYWA2"}}
res1 = requests.post(u1, headers=headers, json=payload1).json()
id2 = ''
src = ''
title = ''
if 'Suha' == T:
title = res1['name']
id2 = res1['hdp']['id']
src = res1['hdp']['src']
elif 'Shiy' == T:
title = res1['title']
id2 = res1['hdp']['id']
src = res1['hdp']['src']
payload2 = {"Id": id2, "context": {"tourToken": tourToken, "appKey": "CAGWEB", "appSec": "ZETYK0B8KTQB41KYWA2"}}
if src == 'PIC':
res = requests.post(u2, headers=headers, json=payload2).json()
resourceId = res['resourceId']
name = res['name']
maxlevel = res['maxlevel']
size = res['size']
h = size['height']
w = size['width']
download_and_paste_image(w, h, title, name, resourceId, maxlevel)
else:
res = requests.post(u3, headers=headers, json=payload2).json()
for i in res['data']:
resourceId = i['resourceId']
name = i['name']
maxlevel = i['maxlevel']
size = i['size']
h = size['height']
w = size['width']
download_and_paste_image(w, h, title, name, resourceId, maxlevel)
def download_and_paste_image(w, h, title, name, id, maxlevel):
if title == name:
t_m = name
else:
t_m = title + '_' + name
new_img = Image.new('RGB', (w, h))
for i in range(0, w // 512 + 1):
for j in range(0, h // 512 + 1):
p_url = 'https://cags.ltfc.net/cagstore/{}/{}/{}_{}.jpg'.format(id, maxlevel, i, j)
response = requests.get(p_url)
response.raise_for_status()# 如果请求失败,则抛出HTTPError异常
# with open(f'{i}_{j}.jpg', 'wb') as f:# 取消注释下载切割后的图片片段到本地
# f.write(response.content)
img = Image.open(BytesIO(response.content))
new_img.paste(img, (512 * i, 512 * j))
new_img.save(t_m + '.jpg')
new_img.close()
del new_img
if __name__ == "__main__":
url = input('输入网址:')
main(url)
刚好需要,点了 isjeffery 发表于 2024-10-12 20:45
大佬,请问一下执行这个文件后就能自动合并下载吗?下载会保存在哪个位置。
保存在脚本目录,自动合并,是先下载到内存,再合并,可能所需时间有点长,你可以把64-65行取消注释,就把所有的分片下载到本地了 感谢分享,点了 谢谢分享,刚好学习一下。 谢谢分享 楼主辛苦 学习一下 刚需,感谢分享 谢谢分享,刚好学习一下 多谢分享 学习一下