Python:我的 except 子句确实太宽泛了吗?

标签 python html beautifulsoup pycharm try-except

我的代码工作正常,但 PyCharm 警告 except以下条款过于宽泛。老实说,我也觉得这是错误的实现。

抓取 HTML 时 <tr>是批量的,我还必须添加到数据库或更新,我事先不知道游戏是否完成。另外,尚未完成的游戏在某些 <td> 中具有不同的 HTML 标签。 s,需要不同的处理。

基本上,如果我们看 match_score_stringmatch_relevant_bit我们告诉 BeautifulSoup 找到一个 <td>与某个类(class)。如果游戏已经完成,则此<td>将有一个score-time sc类,如果没有,它将有一个 score-time st类(class)。如果有一个,就不会再有另一个。

自从我写try-except以来已经有一段时间了。子句,但如果我没记错的话,(我觉得)我必须使用它们的原因是因为 BS 会在 row.find 时抛出错误并停止所有操作。无法定位 HTML 对象。

PyCharm 的提示是否合理?

# Get Match Score
try:
    match_score_string = row.find("td", class_="score-time sc").get_text()
    match_string_split = match_score_string.split(" - ")
    team_a_score = int(match_string_split[0])
    team_b_score = int(match_string_split[1])
    print(team_a_score)
    print(team_b_score)
except:
    team_a_score = None
    team_b_score = None

# Get Match URL
try:
   match_relevant_bit = row.find("td", class_="score-time sc")
   match_url = match_relevant_bit.find("a").get("href")
   match_url_done = match_url.rsplit('?JKLMN')
   match_url = match_url_done[0]
   match_finished = True

except:

   match_relevant_bit = row.find("td", class_="score-time st")
   match_url = match_relevant_bit.find("a").get("href")
   match_url_done = match_url.rsplit('?JKLMN')
   match_url = match_url_done[0]
   match_finished = False

最佳答案

你的 except肯定太宽泛了。我不会争论在这些情况下是否需要异常处理——异常非常Pythonic,所以在你认为合适的地方使用它们。但是,您并没有争论您需要代码来处理所有可以想象到的异常。一个裸露的 except 可以掩盖各种错误。例如如果您在以后的版本中拼错了变量名称:

try:
    match_score_string = row.find("td", class_="score-time sc").get_text()
    match_string_split = match_score_string.split(" - ")
    team_a_score = int(match_string_spilt[0])  # oops
    team_b_score = int(match_string_split[1])
    print(team_a_score)
    print(team_b_score)
except:
    team_a_score = None
    team_b_score = None

纯粹的异常(exception)也会阻止用户发送KeyboardInterrupt来终止程序...

这只是为什么你不想要一个裸露的 except 的两个例子 - 由于可能由此掩盖的编程错误的数量接近无限,我没有足够的时间来演示所有这些错误;-) .

相反,您应该准确指定您期望遇到的异常:

try:
    match_score_string = row.find("td", class_="score-time sc").get_text()
    match_string_split = match_score_string.split(" - ")
    team_a_score = int(match_string_spilt[0])  # oops
    team_b_score = int(match_string_split[1])
    print(team_a_score)
    print(team_b_score)
except AttributeError:  # If no row is found, accessing get_text fails.
    team_a_score = None
    team_b_score = None

这样,代码就会按照您的预期执行,并且您不会掩盖(太多)编程错误。为了防止隐藏错误,您要做的下一件事是尽可能限制异常处理程序中的代码量。

关于Python:我的 except 子句确实太宽泛了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41357717/

相关文章:

python - 提取 URL 的特定部分

python - 在python上使用selenium或beautifulsoup从带有链接的页面中抓取数据,没有类,没有id

python apscheduler - 跳过 : maximum number of running instances reached

python - 获取从 random.sample(list,2) 检索到的值的索引?

python - 如何使用 tweepy-python 获取用户喜欢(收藏)的推文?

python - 在Python中执行多线程程序以单线程运行

javascript - 如何克服 Import.io 对具有更多 JavaScript 代码的网站的问题?

html - 复制带有一些变化的行的正则表达式,镜像

javascript - 使用javascript在浏览器调整大小时更改背景css

python - 迭代可变数量的表行时出现 IndexError