The code's below. And I get "urllib.error.HTTPError: HTTP Error 404: Not Found".
import urllib.request
import urllib.parse
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686)"
values = {'q': 'python programming tutorials'}
data = urllib.parse.urlencode(values)
url = '
req = urllib.request.Request(url, headers = headers)
resp = urllib.request.urlopen(req)
resp_data = resp.read()
print(resp_data) 1 Answer
Your problem is that you're not adding data as a query parameter, indicated by adding a ? after /search
Here is your code modified and working
import urllib.request
import urllib.parse
headers = {}
headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686)"
values = {'q': 'python programming tutorials'}
data = urllib.parse.urlencode(values)
url = '
req = urllib.request.Request(url, headers = headers)
resp = urllib.request.urlopen(req)
resp_data = resp.read()
print(resp_data)The actual difference being your url
>>> url
'and my modified url
>>> url
'There is no resource located at and that is the reason you're getting
urllib.error.HTTPError: HTTP Error 404: Not Found