fix(api): 修复因缺少“电子邮箱”字段导致的 KeyError#24
Closed
NianBroken wants to merge 1 commit intoopenschoolcn:mainfrom
Closed
Conversation
在 `_get_info` 函数中,当解析的网页不包含“电子邮箱”等可选字段时,
`pending_result` 字典中会缺少对应的键。
旧代码的逻辑 `("无" if pending_result.get("电子邮箱:") == "" else pending_result["电子邮箱:"])` 存在缺陷:
当键不存在时,`.get()` 返回 `None`,导致条件判断为 `False`,
进而执行 `else` 分支中的 `pending_result["电子邮箱:"]`,
这种不安全的方括号访问方式直接引发了 `KeyError` 异常,导致程序崩溃。
本次提交通过将所有类似的数据提取逻辑统一修改为更健壮的 `pending_result.get(key) or "无"` 模式,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 "无"模式,