Answer the question
In order to leave comments, you need to log in
What is the correct way to cast a try except exception on requests.exceptions.HTTPError?
I'm slowly learning Python.
For example, if you run the following command
import requests
r = requests.get('https://toster.ru/q/qwerty123v')
r.raise_for_status()
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "C:\Users\Marat\Documents\first.py", line 8, in <module>
r.raise_for_status()
File "C:\Python34\lib\site-packages\requests\models.py", line 840, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://toster.ru/q/qwerty123v
r.headers['content-type']
Answer the question
In order to leave comments, you need to log in
Why are you using raise_for_status?
I may not understand the terms, but something like this
import requests
test_urls = set([
'http://habrahabr.ru',
'https://toster.ru/q/qwerty123v'
])
for url in test_urls:
r = requests.head(url)
print("{:<50}: {}".format(url, r.headers['content-type']))
from urllib.error import HTTPError
try:
r.raise_for_status()
except HTTPError:
continue
finally:
Ваш код обработки
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question