'commit'
This commit is contained in:
@@ -30,29 +30,15 @@ logger = logging.getLogger("AiTeJiYiChongCrawler")
|
||||
|
||||
async def is_already_crawled(redis_kit, station_name):
|
||||
"""
|
||||
检查场站是否已经爬取过,支持前缀模糊匹配处理截断名称
|
||||
检查场站是否已经爬取过,仅按名称去重
|
||||
"""
|
||||
cleaned_name = Kit.clean_station_name(station_name)
|
||||
if not cleaned_name:
|
||||
return False
|
||||
|
||||
# 1. 检查精确匹配
|
||||
redis_key = f"crawled:aite:{cleaned_name}"
|
||||
if await redis_kit.get_data(redis_key):
|
||||
return True
|
||||
|
||||
# 2. 如果名称被截断(原名包含 . ),尝试前缀匹配
|
||||
# 逻辑:在 Redis 中查找是否有以当前清理后的名称开头的已完成场站
|
||||
# 为了效率,我们配合 Redis 的 keys 模式(小规模数据下可用)
|
||||
if ".." in station_name or station_name.endswith("."):
|
||||
# 获取所有已爬取的 key 进行内存前缀比对,或者利用 Redis 模糊查询
|
||||
# 这里采用一种更高效的方式:在详情页保存时,额外保存一个前缀索引
|
||||
prefix_key = f"crawled:aite:prefix:{cleaned_name[:6]}" # 取前6位作为索引前缀
|
||||
stored_names = await redis_kit.get_data(prefix_key)
|
||||
if stored_names and isinstance(stored_names, list):
|
||||
for full_name in stored_names:
|
||||
if full_name.startswith(cleaned_name):
|
||||
return True
|
||||
return False
|
||||
|
||||
async def get_station_list(d, service, max_scrolls=MAX_SCROLLS):
|
||||
@@ -190,16 +176,9 @@ async def get_station_list(d, service, max_scrolls=MAX_SCROLLS):
|
||||
d.press("back")
|
||||
await asyncio.sleep(0.8)
|
||||
|
||||
# 记录 Redis (保存全名并建立前缀索引)
|
||||
# 记录 Redis (仅按名称去重,不再使用前缀索引)
|
||||
full_name_key = f"crawled:aite:{Kit.clean_station_name(station_name)}"
|
||||
await redis_kit.set_data(full_name_key, "1", expire=REDIS_STATION_EXPIRE)
|
||||
|
||||
# 建立前缀索引 (取前6位,方便模糊查找)
|
||||
prefix_idx_key = f"crawled:aite:prefix:{Kit.clean_station_name(station_name)[:6]}"
|
||||
existing_names = await redis_kit.get_data(prefix_idx_key) or []
|
||||
if station_name not in existing_names:
|
||||
existing_names.append(station_name)
|
||||
await redis_kit.set_data(prefix_idx_key, existing_names, expire=REDIS_STATION_EXPIRE)
|
||||
|
||||
# 在每一页结束时,清理已完成的任务,防止内存堆积
|
||||
done_tasks = [t for t in background_tasks if t.done()]
|
||||
|
||||
Reference in New Issue
Block a user