是否有理由优先引发/捕获requests.exceptions.HTTPError而不是requests.HTTPError或requests.HTTPError而不是requests.exceptions.HTTPError
这两个是同一个类(通过阅读源代码和见证True是requests.exceptions.HTTPError is requests.HTTPError的输出可以看出这一点),但是我问这个问题,因为它们都存在。
发布于 2021-04-02 01:11:33
它们都做同样的事情。没有理由将其中一个置于另一个之上。
试试这个实验:
try:
r = requests.get('http://www.google.com/nothere')
r.raise_for_status()
except requests.exceptions.HTTPError as e:
print(e.response.text) 然后:
try:
r = requests.get('http://www.google.com/nothere')
r.raise_for_status()
except requests.HTTPError as e:
print(e.response.text) 你每次都会得到同样的东西:
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
[***]
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/nothere</code> was not found on this server.
<ins>That’s all we know.</ins>https://stackoverflow.com/questions/66908549
复制相似问题