-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch_4.py
More file actions
43 lines (37 loc) · 1.23 KB
/
Copy pathscratch_4.py
File metadata and controls
43 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import requests
from urllib.parse import urljoin, urlparse
import time as t
# 基础URL
base_url = 'https://gwent.one/img/assets/max/number/'
# 创建存储图片的目录
def create_dir_if_not_exists(dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
# 下载图片并保存到对应目录
def download_image(image_url, category, index):
# 完整的图片URL
full_url = urljoin(base_url, image_url)
# 构建保存路径
save_path = os.path.join(category, f'{index}.png')
# 确保保存目录存在
create_dir_if_not_exists(category)
# 下载图片
response = requests.get(full_url)
t.sleep(0.5)
with open(save_path, 'wb') as file:
file.write(response.content)
print(f'Downloaded {full_url} to {save_path}')
# 生成图片URL并下载
def download_images(start, end, category):
for i in range(start, end + 1):
image_name = f'{category}_{i}.png' if i >= 10 else f'{category}_0{i}.png'
image_url = image_name
download_image(image_url, category, i)
# 主函数
def main():
categories = ['provision', 'armor', 'power', 'ability']
for category in categories:
download_images(0, 20, category)
if __name__ == '__main__':
main()