mirror of
https://github.com/NanmiCoder/MediaCrawler.git
synced 2026-10-07 02:48:12 +08:00
fix(ks): 视频不可用时 photo 为 null 导致整轮爬取崩溃
快手对已删除/私密/不存在的视频,返回的 visionVideoDetail 里 photo/author 是
null——key 存在、值是 null。而 detail.get("photo", {}) 只在 key 缺失时给默认值,
key 存在且为 null 时拿到的仍是 None,紧接着的 photo.get(...) 抛 AttributeError。
该异常不在 get_video_info_task 的 except 列表里(只 catch DataFetchError /
KeyError),会穿过 asyncio.gather 直接把整轮爬取带崩。
- core.py: 用 (x or {}) 兜住 null;photo 为空时打 WARNING 并跳过该视频,
不再返回半残的 detail 让下游存空记录、再去下载媒体
- 新增 tests/test_kuaishou_unavailable_video.py(不发网络请求)
验证:photo=null / photo 缺失 / visionVideoDetail 缺失 均返回 None;
真实 cookie 端到端确认不可用视频被跳过、正常视频照常返回详情。
This commit is contained in:
@@ -237,15 +237,29 @@ class KuaishouCrawler(AbstractCrawler):
|
||||
utils.logger.info(f"[KuaishouCrawler.get_video_info_task] Sleeping for {sleep_sec:.1f} seconds after fetching video details {video_id}")
|
||||
|
||||
detail = result.get("visionVideoDetail")
|
||||
if detail:
|
||||
photo = detail.get("photo", {})
|
||||
author = detail.get("author", {})
|
||||
utils.logger.info(
|
||||
f"[KuaishouCrawler.get_video_info_task] video detail: "
|
||||
f"id={photo.get('id', video_id)} author={author.get('name', '')} "
|
||||
f"likes={photo.get('likeCount', '')} views={photo.get('viewCount', '')} "
|
||||
f"caption={str(photo.get('caption', ''))[:50]}"
|
||||
if not detail:
|
||||
return None
|
||||
|
||||
# 快手对不可用视频(已删除/私密/不存在)返回的是
|
||||
# visionVideoDetail: {photo: null, author: null}——key 在、值是 null。
|
||||
# 注意 .get("photo", {}) 只在 key **缺失** 时给默认值,key 存在且为 null
|
||||
# 时拿到的仍是 None,接着 .get() 就抛 AttributeError,而
|
||||
# asyncio.gather 不会拦住它,整轮爬取会直接带崩。
|
||||
photo = detail.get("photo") or {}
|
||||
if not photo:
|
||||
utils.logger.warning(
|
||||
f"[KuaishouCrawler.get_video_info_task] 视频不可用"
|
||||
f"(photo 为空,可能已删除或私密),跳过 video_id={video_id}"
|
||||
)
|
||||
return None
|
||||
|
||||
author = detail.get("author") or {}
|
||||
utils.logger.info(
|
||||
f"[KuaishouCrawler.get_video_info_task] video detail: "
|
||||
f"id={photo.get('id', video_id)} author={author.get('name', '')} "
|
||||
f"likes={photo.get('likeCount', '')} views={photo.get('viewCount', '')} "
|
||||
f"caption={str(photo.get('caption', ''))[:50]}"
|
||||
)
|
||||
return detail
|
||||
except DataFetchError as ex:
|
||||
utils.logger.error(
|
||||
|
||||
Reference in New Issue
Block a user