本帖最后由 hebeijianke 于 2024-7-26 21:29 编辑
看到求助帖有人需要 @幸甚至哉
代码比较简单,只能下载较小的图片,图像的宽度和高度小于65500像素,好像python的几个图像处理模块都对拼接大图有限制,
注意:只能下载【书画】和【拾英】板块免费的,需要会员的不能下载,单线程下载,有点慢。
希望大佬优化一下代码吧。
[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)
|