我正在编写一个 url getter 。当我发送如下请求时:
import requests
response = requests.get("http://example.com")
有时会出现这样的错误:
ConnectionError: ('Connection aborted.', BadStatusLine(""''''"))
但是当我再尝试一次时,问题就解决了。所以当再次出现这样的错误时我想再发送一次。我怎样才能做到这一点?预先感谢您!
最佳答案
当异常发生时再次重复请求。
import requests
url = "http://example.com"
try:
response = requests.get(url)
except requests.exception.ConnectionError:
response = requests.get(url)
关于python - python-requests 一次失败后如何再发送一个请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162673/