last line of code is giving error.

import re
import urllib.request

url="" data=urllib.request.urlopen(url).read() Traceback (most recent call last): File "", line 1, in data=urllib.request.urlopen(url).read() File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 222, in urlopen return, data, timeout) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 531, in open response = meth(req, response) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 641, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 569, in error return self._call_chain(*args) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 503, in _call_chain result = func(*args) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\", line 649, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden

I want to open the url. Url works when i open manually. I do not understand why i get this error and what does this error mean?

1 Answers

Community On

The HTTP Error 403: Forbidden error probably caused by remote server security for blocking bots user agents, changing the user-agent headers will fix this error.

from urllib.request import Request, urlopen

req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})