-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch_18.py
More file actions
59 lines (52 loc) · 2 KB
/
Copy pathscratch_18.py
File metadata and controls
59 lines (52 loc) · 2 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import re
import time
import requests
import csv
# 读取CSV文件并提取data-artid列的值
csv_file_path = r".\cards.csv" # 替换为你的CSV文件路径
art_ids = set()
with open(csv_file_path, newline='', encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
art_id = row['data-artid'].rstrip('j') # 假设CSV中的artid后缀是'j',根据实际情况调整
art_ids.add(art_id)
print(len(art_ids))
# 读取CSS文件
css_file_path = r"./assets\cards_html\asset-v9.css" # 替换为你的CSS文件路径
with open(css_file_path, "r", encoding="utf-8") as f:
css_content = f.read()
# 正则表达式匹配CSS中的图片URL
img_url_pattern = re.compile(r'url\((https?://[^\)]+)\)')
# 查找所有图片链接并过滤,只下载CSV文件中存在的art_id对应的图片
img_urls = img_url_pattern.findall(css_content)
download_folder = r"./assets\download_imgs"
# 确保下载文件夹存在
if not os.path.exists(download_folder):
os.makedirs(download_folder)
c = 0
# 下载并保存图片
for url in []:
c += 1
print(f"{c}/{len(img_urls)}")
# 从URL中提取图片编号
try:
response = requests.get(url)
img_name = f"{url.split('/')[-1]}"
with open(os.path.join(download_folder, img_name), 'wb') as f:
f.write(response.content)
print(f"Downloaded {img_name} to {download_folder}, {c}/{len(art_ids)}")
except Exception as e:
print(f"Failed to download {url}. Error: {e}")
time.sleep(1)
# 更新CSS中的图片链接
updated_css_content = css_content
for url in img_urls:
if url in css_content: # 确保URL存在于CSS内容中
match = re.search(r'/(\d+).png', url)
img_name = f".\\{url.split('/')[-1]}.png"
updated_css_content = updated_css_content.replace(url, img_name)
# 将更新后的CSS内容写回到文件
with open(css_file_path, 'w') as file:
file.write(updated_css_content)
print("All relevant images have been downloaded and CSS updated.")