Urllib. Error. Httperror: Http Error 404: Not Found

Urllib. Error. Httperror: Http Error 404: Not Found

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.