fix(api): 修复因缺少“电子邮箱”字段导致的 KeyError #24
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
在
_get_info函数中,当解析的网页不包含“电子邮箱”等可选字段时,pending_result字典中会缺少对应的键。旧代码的逻辑
("无" if pending_result.get("电子邮箱:") == "" else pending_result["电子邮箱:"])存在缺陷: 当键不存在时,.get()返回None,导致条件判断为False,进而执行
else分支中的pending_result["电子邮箱:"],这种不安全的方括号访问方式直接引发了
KeyError异常,导致程序崩溃。本次提交通过将所有类似的数据提取逻辑统一修改为更健壮的
pending_result.get(key) or "无"模式,