last line of code is giving error.

import re
import urllib.request
url="https://www.google.com/search?q=google&tbm=fin#scso=_GYPEXIHYJs6gtQXFn7i4Aw2:0"
data=urllib.request.urlopen(url).read()

url="https://www.google.com/search?q=google&tbm=fin#scso=_GYPEXIHYJs6gtQXFn7i4Aw2:0" 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\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 531, in open response = meth(req, response) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 641, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 569, in error return self._call_chain(*args) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\SHARM\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py", 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

0
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

url="https://www.google.com/search?q=google&tbm=fin#scso=_GYPEXIHYJs6gtQXFn7i4Aw2:0" 
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
data=urlopen(req).read()