谁有空,下载剩余的吧。下载代码python:
[Python] 纯文本查看 复制代码 import os
import requests
from requests.exceptions import RequestException
def create_folder(folder_name):
"""创建文件夹,如果文件夹已存在则不执行任何操作"""
if not os.path.exists(folder_name):
os.makedirs(folder_name)
def download_image(url, save_path1):
"""下载图片并保存到指定路径,同时附带headers"""
try:
response = requests.get(url, stream=True, headers=headers)
response.raise_for_status() # 如果请求失败,抛出HTTPError异常
with open(save_path1, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
except RequestException as e:
print(f"请求图片时发生错误: {e}")
except Exception as e:
print(f"保存图片时发生错误: {e}")
# sh = [20170600 + i for i in range(1, 31)] + [20170700 + i for i in range(1, 27)] + [20210705, 20220101]
sh = [20170700 + i for i in range(1, 27)] + [20210705, 20220101]
ljs = []
for s in sh:
for i in range(1, 999):
ljs.append(f'http://wsk.ycszx.gov.cn:8888/yczx/papge/sxrb/{s}/{str(i).zfill(4)}.jpg')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'}
for link in ljs:
bt = link.split('sxrb/')[1].split('/')[0]
image_folder = f"d:/a/{bt}" # 图片文件夹名
create_folder(image_folder) # 创建图片文件夹
img = link
print(img)
save_path = os.path.join(image_folder, f"image_{link[-8:]}")
download_image(img, save_path)
|