from urllib.request import Request, urlopen from urllib.error import URLError, HTTPError
url = 'https://www.alura.com.br' headers = {'User-Agent' : 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0'}
try: req = Request(url, headers = headers) response = urlopen(req) print(response.read())
except: HTTPError as e print(e.status, e.reason)
Output:
File "", line 12 print(response.read()) ^ SyntaxError: unexpected EOF while parsing except: URLError as e print(e.status,e.reason)