利用Python批量下载RSS源图片集
2025-06-28 04:23:53

⚠️ 注意事项

  • 请尊重网站的版权和 robots.txt 协议。
  • 请勿将并发数设置得过高,过度频繁的请求可能会给目标服务器带来较大负担,甚至导致你IP被封禁。
  • 本脚本仅供学习和个人收藏用途,请勿用于非法商业活动。

📁 文件结构

在运行前,请确保您的项目文件夹包含以下文件:

1
2
3
4
5
6
7
8
.
├── downloader.py # 主程序脚本
├── config.yaml # 配置文件
├── requirements.txt # Python 依赖库
├── feeds.opml # 你的 RSS 订阅列表 (需自行准备)

├── history.db # (程序首次运行后自动生成) 下载历史数据库
└── downloader_errors.log # (出现错误后自动生成) 错误日志文件

(可选文件)

1
└── cookies.txt            # (可选) 用于模拟登录的Cookie文件

🐍 主程序

downloader.py

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import asyncio
import os
import re
import time
import feedparser
import aiohttp
import aiosqlite
import yaml
import html
import argparse
import logging
import random
from bs4 import BeautifulSoup
from urllib.parse import urlparse, urljoin
from tqdm.asyncio import tqdm as aio_tqdm
from http.cookies import SimpleCookie

# ==============================================================================
# 日志设置
# ==============================================================================
logger = logging.getLogger("下载器")

def setup_loggers(console_level=logging.INFO, error_file='downloader_errors.log'):
"""配置控制台和文件日志记录器。"""
if logger.hasHandlers():
logger.handlers.clear()

logger.setLevel(logging.DEBUG)

# 1. 控制台处理器
console_handler = logging.StreamHandler()
console_handler.setLevel(console_level)
console_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - [%(name)s] - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)

# 2. 错误文件处理器
try:
file_handler = logging.FileHandler(error_file, 'a', encoding='utf-8')
file_handler.setLevel(logging.WARNING)
file_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
)
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
logger.info(f"错误日志将记录到: {os.path.abspath(error_file)}")
except Exception as e:
logger.error(f"无法创建错误日志文件 {error_file}: {e}")

# ==============================================================================
# 配置加载与正则表达式预编译
# ==============================================================================
CONFIG = {}
REGEX = {
"image_file_ext": re.compile(r'\.(jpg|jpeg|png|webp|gif)$', re.I),
"blogspot_thumb": re.compile(r'/s\d+(-[a-z])?/', re.I),
"thumbnail_resize": re.compile(r'-\d+x\d+(\.\w+)$'),
}

def load_config(path='config.yaml'):
"""从 YAML 文件加载配置。"""
global CONFIG
try:
with open(path, 'r', encoding='utf-8') as f:
CONFIG = yaml.safe_load(f)
setup_loggers(error_file=CONFIG.get('error_log_file', 'downloader_errors.log'))
logger.info("成功从 %s 加载配置。", path)
except FileNotFoundError:
print(f"致命错误: 未找到 {path} 文件,请创建它。")
exit(1)
except Exception as e:
print(f"致命错误: 加载 {path} 出错: {e}")
exit(1)

# ==============================================================================
# 数据库与Cookie (异步)
# ==============================================================================
class DatabaseManager:
"""异步管理用于存储下载历史的 SQLite 数据库 (基于文章标题全局去重)。"""
def __init__(self, db_path):
self._db_path = db_path
self._conn = None

async def connect(self):
try:
self._conn = await aiosqlite.connect(self._db_path)
await self._conn.execute("PRAGMA journal_mode=WAL;")
await self._conn.execute("""
CREATE TABLE IF NOT EXISTS download_history (
post_title TEXT PRIMARY KEY,
download_date TEXT NOT NULL,
source_feed TEXT
)
""")
await self._conn.commit()
logger.info("数据库连接成功 (全局去重模式): %s", self._db_path)
except Exception as e:
logger.error("数据库连接失败: %s", e)
raise

async def is_downloaded(self, post_title):
"""检查一个文章标题是否已经被下载过。"""
async with self._conn.execute(
"SELECT 1 FROM download_history WHERE post_title = ?",
(post_title,)
) as cursor:
return await cursor.fetchone() is not None

async def add_entry(self, post_title, feed_title):
"""将一个文章标题添加到下载历史中。"""
now = time.strftime('%Y-%m-%d %H:%M:%S')
try:
await self._conn.execute(
"INSERT OR IGNORE INTO download_history (post_title, download_date, source_feed) VALUES (?, ?, ?)",
(post_title, now, feed_title)
)
await self._conn.commit()
except Exception as e:
logger.error("添加记录 '%s' 到数据库失败: %s", post_title, e)

async def close(self):
if self._conn:
await self._conn.close()
logger.info("数据库连接已关闭。")

class NetscapeCookieJar(aiohttp.CookieJar):
"""一个可以从 Netscape 格式的 cookies.txt 文件加载 cookie 的 CookieJar。"""
def __init__(self, cookie_file=None):
super().__init__()
if cookie_file and os.path.exists(cookie_file):
self.load_from_file(cookie_file)

def load_from_file(self, cookie_file):
try:
with open(cookie_file, 'r') as f:
for line in f:
if line.strip().startswith('#') or not line.strip():
continue

try:
domain, _, path, secure, expires, name, value = line.strip().split('\t')
cookie = SimpleCookie()
cookie[name] = value
cookie[name]['path'] = path
cookie[name]['domain'] = domain
cookie[name]['expires'] = int(float(expires))
cookie[name]['secure'] = secure.upper() == 'TRUE'
self.update_cookies(cookie)
except ValueError:
logger.warning(f"无法解析Cookie行: {line.strip()}")
logger.info(f"成功从 {cookie_file} 加载 Cookies。")
except Exception as e:
logger.error(f"加载Cookies文件 {cookie_file} 失败: {e}")

# ==============================================================================
# 工具函数
# ==============================================================================
def sanitize_folder_name(name):
sanitized = html.unescape(name).strip()
invalid_chars = r'[\\/:*?"<>|]'
sanitized = re.sub(invalid_chars, '_', sanitized)
return sanitized or "未命名"

def get_full_image_url(url):
if "bp.blogspot.com" in url or "googleusercontent.com" in url:
return REGEX["blogspot_thumb"].sub('/s0/', url)
if REGEX["thumbnail_resize"].search(url):
return REGEX["thumbnail_resize"].sub(r'\1', url)
return url

# ==============================================================================
# 网络与下载核心 (异步)
# ==============================================================================
async def fetch(session, url, log_prefix=""):
for attempt in range(CONFIG.get('max_retries', 3) + 1):
try:
async with session.get(url, timeout=CONFIG.get('request_timeout', 45), headers=CONFIG.get('request_headers', {})) as response:
response.raise_for_status()
return await response.text()
except (aiohttp.ClientError, asyncio.TimeoutError) as e:
if attempt < CONFIG.get('max_retries', 3):
delay = CONFIG.get('retry_delay', 2) * (2 ** attempt)
logger.warning("%s第 %d/%d 次抓取 %s 失败。将在 %.1f 秒后重试...", log_prefix, attempt + 1, CONFIG.get('max_retries', 3) + 1, url, delay)
await asyncio.sleep(delay)
else:
logger.error("%s抓取 %s 在 %d 次重试后仍然失败: %s", log_prefix, url, CONFIG.get('max_retries', 3) + 1, e)
return None

async def download_image(session, img_url, filepath, referer_url, log_prefix=""):
"""异步下载单张图片,并在失败时记录详细错误。"""
full_img_url = get_full_image_url(img_url)
headers = CONFIG.get('image_headers', {}).copy()
headers['Referer'] = referer_url
last_exception = None

for attempt in range(CONFIG.get('max_retries', 3) + 1):
try:
async with session.get(full_img_url, timeout=CONFIG.get('request_timeout', 45), headers=headers) as response:
if response.status == 404 and full_img_url != img_url:
logger.warning("%s高清图URL %s 返回404,尝试原始URL: %s", log_prefix, full_img_url, img_url)
return await download_image(session, img_url, filepath, referer_url, log_prefix)

response.raise_for_status()

content = await response.read()
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, 'wb') as f:
f.write(content)
return True, os.path.basename(filepath)

except (aiohttp.ClientError, asyncio.TimeoutError) as e:
last_exception = e
if attempt < CONFIG.get('max_retries', 3):
delay = CONFIG.get('retry_delay', 5) * (2 ** attempt)
await asyncio.sleep(delay)
else:
# 最终失败时记录详细错误
error_message = f"重试 {CONFIG.get('max_retries', 3) + 1} 次后失败。URL: {img_url}"
logger.error("%s%s, 底层错误: %s", log_prefix, error_message, last_exception, exc_info=False)
return False, str(last_exception)
return False, "已达最大重试次数"

async def fetch_paginated_content(session, url, log_prefix=""):
"""异步获取一个分页文章的所有页面HTML内容。"""
full_html = ""
current_url = url
processed_urls = set()
while current_url and current_url not in processed_urls:
logger.debug("%s正在抓取分页内容: %s", log_prefix, current_url)
processed_urls.add(current_url)
html_content = await fetch(session, current_url, log_prefix)
if not html_content:
break
soup = BeautifulSoup(html_content, 'html.parser')
content_area = soup.select_one('div.entry-content, div.post-content, article.post, div#content')
if content_area:
full_html += str(content_area)
else:
full_html += html_content
next_page_link = soup.select_one('a.next.page-numbers, a[rel=next], .pagination-next a, a:-soup-contains("Next Page")')
if next_page_link and next_page_link.get('href'):
current_url = urljoin(current_url, next_page_link['href'])
await asyncio.sleep(random.uniform(0.5, 2.0)) # 抓取分页时也增加随机延迟
else:
current_url = None
return full_html

async def extract_images_from_page(session, article_url, log_prefix=""):
"""(更精确版) 从一个可能分页的文章中提取所有图片URL。"""
full_html_content = await fetch_paginated_content(session, article_url, log_prefix)
if not full_html_content:
return []

soup = BeautifulSoup(full_html_content, 'html.parser')
image_urls = set()
content_area = soup.select_one('div.entry-content, div.post-content, article.post, div#content')
search_area = content_area if content_area else soup

for img in search_area.find_all('img'):
src = img.get('data-src') or img.get('src')
if src:
full_url = urljoin(article_url, src.strip())
if REGEX["image_file_ext"].search(full_url.split('?')[0]):
image_urls.add(full_url)

return list(image_urls)

# ==============================================================================
# 主要处理逻辑 (异步)
# ==============================================================================
async def process_entry(session, db, entry, feed_title):
"""处理单个 RSS 条目,并记录下载失败 (基于文章标题全局去重)。"""
entry_id = getattr(entry, 'id', entry.link)
article_url = entry.link
log_prefix = f"[{feed_title[:15]}] "

# 1. 首先获取并清理标题
post_title = sanitize_folder_name(entry.title)

# 2. 使用清理后的标题进行全局重复检查
if not article_url or await db.is_downloaded(post_title):
if not article_url:
return 0, 0
logger.info("%s文章 '%s' 已在全局历史中存在,跳过。", log_prefix, post_title)
return 0, 0

# 如果没跳过,说明是新文章,继续处理
logger.info("%s处理新文章: '%s'", log_prefix, post_title)

image_urls = []
html_description = getattr(entry, 'description', '')

if html_description:
soup = BeautifulSoup(html_description, 'html.parser')
gallery_div = soup.select_one('div.gallery, div#gallery')
if gallery_div:
links = gallery_div.select('a')
for link in links:
href = link.get('href')
if href and REGEX["image_file_ext"].search(href.split('?')[0]):
image_urls.append(urljoin(article_url, href.strip()))

if not image_urls:
for img in soup.find_all('img'):
src = img.get('src') or img.get('data-src')
if src:
image_urls.append(urljoin(article_url, src.strip()))

if not image_urls and CONFIG.get('allow_fallback_to_source_site', False):
logger.warning("%s在RSS源中未找到图片,将访问源网站: %s", log_prefix, article_url)
image_urls = await extract_images_from_page(session, article_url, log_prefix)
elif image_urls:
unique_urls = list(dict.fromkeys(image_urls))
image_urls = unique_urls
logger.info("%s已成功从RSS源描述中提取 %d 个图片地址。", log_prefix, len(image_urls))

if not image_urls:
logger.warning("%s文章 '%s' 未能找到任何图片。", log_prefix, post_title)
# 将标题添加到数据库
await db.add_entry(post_title, feed_title)
return 1, 0

post_folder = os.path.join(CONFIG['base_folder'], sanitize_folder_name(feed_title), post_title)
extended_log_prefix = f"[{feed_title}][{post_title}] "

tasks = []
for i, img_url in enumerate(image_urls):
ext = os.path.splitext(urlparse(img_url).path)[1]
if not REGEX["image_file_ext"].search(ext):
ext = ".jpg"
filename = f"{i+1:03d}{ext}"
filepath = os.path.join(post_folder, filename)

if not os.path.exists(filepath):
task = asyncio.create_task(download_image(session, img_url, filepath, article_url, extended_log_prefix))
tasks.append(task)
else:
await asyncio.sleep(0.05)

if not tasks:
logger.info("%s所有 %d 张图片均已存在,跳过下载。", extended_log_prefix, len(image_urls))
# 将标题添加到数据库
await db.add_entry(post_title, feed_title)
return 1, 0

# 使用 asyncio.as_completed 来逐个处理下载任务,并加入随机延时
success_count = 0
pbar = aio_tqdm(total=len(tasks), desc=f"{log_prefix}下载 '{post_title[:20]}…'", unit="张", leave=False)
for task in asyncio.as_completed(tasks):
result, _ = await task
if result:
success_count += 1
pbar.update(1)
# 关键优化:在每次下载后都加入一个随机的短暂延时
await asyncio.sleep(random.uniform(0.5, 1.5))
pbar.close()

failed_count = len(tasks) - success_count

if failed_count == 0:
logger.info("%s成功为 '%s' 下载 %d 张新图片。", log_prefix, post_title, success_count)
else:
logger.error("%s'%s' 下载完成,%d 张成功, %d 张失败。详情请查看错误日志文件。", log_prefix, post_title, success_count, failed_count)

# 将标题添加到数据库
await db.add_entry(post_title, feed_title)
return 1, success_count

async def process_feed(session, db, feed_info):
"""
处理单个 RSS 订阅源。
优先尝试正常解析,如果因内容非XML而失败,则自动使用浏览器User-Agent重试。
"""
feed_title, feed_url = feed_info
log_prefix = f"[{feed_title}] "
logger.info("%s开始处理...", log_prefix)

try:
# --- 步骤 1: 首次尝试,不带任何伪装 ---
feed_data = await asyncio.to_thread(feedparser.parse, feed_url)

# --- 步骤 2: 检查首次尝试是否因“反爬虫”失败 ---
if feed_data.bozo and isinstance(feed_data.bozo_exception, feedparser.NonXMLContentType):
logger.warning("%s首次尝试失败,服务器返回了HTML页面。正在切换伪装模式重试...", log_prefix)

# --- 步骤 3: 切换为伪装模式进行重试 ---
agent = CONFIG.get('request_headers', {}).get('User-Agent', 'Mozilla/5.0')
feed_data = await asyncio.to_thread(feedparser.parse, feed_url, agent=agent)

# --- 步骤 4: 检查最终结果 ---
if feed_data.bozo:
logger.warning("%s订阅源可能格式错误或无法访问: %s. 错误: %s", log_prefix, feed_url, feed_data.bozo_exception)

# 后续逻辑保持不变
entry_tasks = [process_entry(session, db, entry, feed_title) for entry in feed_data.entries]
results = await asyncio.gather(*entry_tasks)

total_new_entries = sum(r[0] for r in results)
total_new_images = sum(r[1] for r in results)

logger.info("%s处理完毕。发现 %d 篇新文章,下载了 %d 张新图片。", log_prefix, total_new_entries, total_new_images)
return total_new_entries, total_new_images

except Exception as e:
logger.error("%s处理时发生意外错误: %s", log_prefix, e, exc_info=True)
return 0, 0

async def main():
"""异步主函数,运行下载器。"""
load_config()

os.makedirs(CONFIG['base_folder'], exist_ok=True)
db_path = os.path.join(CONFIG['base_folder'], CONFIG['db_file'])
db = DatabaseManager(db_path)
await db.connect()

try:
# 如果配置了cookie文件则加载,否则忽略
cookie_file = CONFIG.get('cookie_file')
if cookie_file and not os.path.exists(cookie_file):
logger.warning(f"配置了cookie文件 '{cookie_file}' 但文件不存在,将不加载Cookie。")
cookie_jar = aiohttp.CookieJar()
else:
cookie_jar = NetscapeCookieJar(cookie_file)

connector = aiohttp.TCPConnector(limit_per_host=CONFIG.get('max_concurrent_downloads', 8))

async with aiohttp.ClientSession(connector=connector, cookie_jar=cookie_jar) as session:
with open(CONFIG['opml_file'], 'r', encoding='utf-8') as f:
opml_data = f.read()

opml_soup = BeautifulSoup(opml_data, 'xml')
all_feeds = [
(outline.get('text', '未命名源'), outline.get('xmlUrl'))
for outline in opml_soup.find_all('outline') if outline.get('xmlUrl')
]
feeds_to_process = [f for f in all_feeds if f[0] not in CONFIG.get('skip_feeds', [])]
logger.info("在 %s 中发现 %d 个订阅源。将处理 %d 个。", CONFIG['opml_file'], len(all_feeds), len(feeds_to_process))

stats = {'new_entries': 0, 'new_images': 0}

semaphore = asyncio.Semaphore(CONFIG.get('max_concurrent_feeds', 4))

async def run_with_semaphore(feed_info):
async with semaphore:
return await process_feed(session, db, feed_info)

feed_tasks = [run_with_semaphore(feed) for feed in feeds_to_process]

for future in aio_tqdm.as_completed(feed_tasks, total=len(feed_tasks), desc="处理订阅源", unit="个"):
new_entries, new_images = await future
stats['new_entries'] += new_entries
stats['new_images'] += new_images

logger.info("=" * 60)
logger.info("所有订阅源处理完毕。")
logger.info("总计处理新文章: %d 篇", stats['new_entries'])
logger.info("总计下载新图片: %d 张", stats['new_images'])
logger.info("数据库位于: %s", os.path.abspath(db_path))
logger.info("=" * 60)

except FileNotFoundError as e:
logger.error("未找到关键文件: %s", e)
except Exception as e:
logger.critical("主程序发生严重错误: %s", e, exc_info=True)
finally:
await db.close()

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='异步 RSS 图片下载器 (最终优化版 - 模拟人类行为)',
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('--daemon', action='store_true', help='以守护进程模式运行,按固定间隔重复执行。')
parser.add_argument(
'--interval',
type=int,
default=7200,
help='守护进程模式下的检查间隔(秒)。\n默认值: 7200 (2小时)。'
)

args = parser.parse_args()

if not args.daemon:
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("用户中断了程序。正在关闭...")
else:
logger.info(f"守护进程模式已启动,运行间隔为 {args.interval} 秒 ({args.interval / 3600:.1f} 小时)。")
while True:
start_time = time.time()
try:
logger.info("开始新一轮的检查与下载...")
asyncio.run(main())
except KeyboardInterrupt:
logger.info("守护进程被用户中断。正在退出...")
break
except Exception as e:
logger.critical(f"守护进程在执行任务时发生严重错误: {e}", exc_info=True)

end_time = time.time()
elapsed = end_time - start_time
sleep_time = max(60, args.interval - elapsed)

next_run_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + sleep_time))
logger.info(f"本轮任务执行完毕,耗时 {elapsed:.1f} 秒。将在 {sleep_time:.1f} 秒后开始下一轮(预计时间: {next_run_time_str})。")
time.sleep(sleep_time)

⚙️ 配置文件

config.yaml

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
60
61
62
63
64
# ==============================================================================
# 通用设置
# ==============================================================================
# 所有下载内容的根目录
base_folder: "photos"
# 数据库文件名 (将在根目录内创建)
db_file: "history.db"
# 包含 RSS 订阅源的 OPML 文件
opml_file: "feeds.opml"
# 用于存储错误和失败记录的日志文件
error_log_file: "downloader_errors.log"
# 需要跳过处理的订阅源标题列表
skip_feeds:
- "示例:需要跳过的订阅源"

# 是否允许在RSS内容为空时,回退到访问原始网站链接。
# 对于内容完整的RSS源,强烈建议设为 false,可以根除抓取到网站推荐图的问题。
# 如果有一些只提供摘要的RSS源,则需要设为 true。
allow_fallback_to_source_site: false

# ==============================================================================
# 并发与延迟设置
# ==============================================================================
# 同时处理的订阅源最大数量
max_concurrent_feeds: 1
# 每个条目同时下载图片的最大数量
max_concurrent_downloads: 8
# 通用网络请求超时时间 (秒)
request_timeout: 45
# 失败后重试的基础延迟时间 (秒)
retry_delay: 10
# 单个请求失败后的最大重试次数
max_retries: 10

# ==============================================================================
# 网络请求头
# ==============================================================================
# 用于抓取 RSS 和 HTML 页面的请求头
request_headers:
User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
Accept-Language: "en-US,en;q=0.9,zh-CN;q=0.8"

# 用于下载图片的请求头
image_headers:
User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
Accept: "image/webp,image/apng,image/*,*/*;q=0.8"

# ==============================================================================
# 文件夹与文件名规则
# ==============================================================================
folder_name_rules:
max_length: 150
# 需要被替换的特殊字符
replace_chars:
'[': '【'
']': '】'
':': ':'
'*': '*'
'?': '?'
'"': '“'
'<': '<'
'>': '>'
'|': '|'

📡 演示RSS源

feeds.opml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<head>
<title>My RSS Feeds</title>
</head>
<body>
<outline text="🖼️图片画廊">
<outline text="80K - 写真网" type="rss" xmlUrl="https://127.0.0.1" htmlUrl="https://127.0.0.1"/>
</outline>
</body>
</opml>

🔗 脚本依赖

requirements.txt

1
2
3
4
5
6
7
aiohttp[cchardet,aiodns]
aiosqlite
beautifulsoup4
feedparser
PyYAML
tqdm
lxm

🚀 安装与设置

1. 环境准备

安装 Python

2. 获取文件

把创建的所有文件 (.py, .yaml, feeds.opml,.txt) 并将它们放在同一个文件夹中。

3. 安装依赖

进入项目所在的文件夹,打开终端 ,然后运行以下命令来安装所有必需的 Python 库:

1
pip install -r requirements.txt

主要依赖库包括: aiohttp, aiosqlite, beautifulsoup4, feedparser, pyyaml, tqdm, lxm

⚙️ 配置说明

1. 配置订阅源 (feeds.opml)

最关键的一步。从RSS 阅读器(如 Feedly, Inoreader, Freshrss 等)中,将您的订阅源导出为 OPML 文件格式。将导出的文件命名为 feeds.opml 并放入项目文件夹。

提示: 请确保导出的 OPML 文件是 UTF-8 编码,以避免解析错误。

2. 调整主配置 (config.yaml)

打开 config.yaml 文件,根据里面的中文注释修改配置项。最重要的几项是:

  • base_folder: 图片保存的目录。
  • 反爬虫策略相关:
    • max_concurrent_feeds: 同时处理的网站(订阅源)数量。建议首次运行或遇到频繁失败时设为 1
    • max_concurrent_downloads: 同时下载的图片数量。建议设为 18 之间,数值越小越不容易被封锁
    • retry_delay: 每次重试的基础等待时间(秒)。如果频繁失败,可以适当增加此值,例如 10
  • (可选) Cookie 配置:
    • cookie_file: 如果要下载登录后才能访问的内容,可以在此指定 cookies.txt 文件的路径。

▶️ 如何使用

首次运行建议

  1. config.yaml 中的 max_concurrent_feedsmax_concurrent_downloads 都设置为 1
  2. 运行脚本进行测试。
  3. 如果下载稳定,再逐步调高并发数,找到速度和稳定性的平衡点。

启动命令

完成所有配置后,在终端中进入项目文件夹,运行以下命令即可启动脚本:

1. 标准模式: (运行一次后自动退出)

1
python downloader.py

2. 守护进程模式: (使用默认的2小时间隔,在后台持续运行)

1
python downloader.py --daemon

3. 守护进程模式 + 自定义间隔: (每30分钟检查一次)

1
python downloader.py --daemon --interval 1800

脚本将开始读取 feeds.opml,检查数据库历史记录,并下载所有新的图集。您可以在控制台看到详细的进度和日志。

上一页
2025-06-28 04:23:53