HTTP 406 Not Acceptable client error when using urlopen

I’m using urllib.request.urlopen to query the URL For some reason I cannot access the site using the Python module urllib, because I receive the following HTTP Status Code error:

HTTPError: HTTP Error 406: Not Acceptable

Here is the code that I’m using to make this request:

from urllib.request import urlopen
from bs4 import BeautifulSoup

url = ''
html = urlopen(url).read()
soup = BeautifulSoup(html)

I’m unsure what is causing this error and I need assistance in solving this error.

Below is the Stack Trace related to this error:

HTTPError                                 Traceback (most recent call last)
<ipython-input-5-b158a1e893a0> in <module>
----> 1 html = urlopen("").read()
      2 #print(html)
      3 soup = BeautifulSoup(html)
      4 soup.prettify() in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    220     else:
    221         opener = _opener
--> 222     return, data, timeout)
    224 def install_opener(opener): in open(self, fullurl, data, timeout)
    529         for processor in self.process_response.get(protocol, []):
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    533         return response in http_response(self, request, response)
    639         if not (200 <= code < 300):
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    643         return response in error(self, proto, *args)
    567         if http_err:
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    571 # XXX probably also want an abstract factory that knows when it makes in _call_chain(self, chain, kind, meth_name, *args)
    501         for handler in handlers:
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:
    505                 return result in http_error_default(self, req, fp, code, msg, hdrs)
    647 class HTTPDefaultErrorHandler(BaseHandler):
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    651 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 406: Not Acceptable


I’m looking into the 406 error code, which happens when the server cannot respond with the accept-header specified in the request. If I can get urlopen to work correctly, I will post that answer too.

I don’t get this error when using Python Requests

import requests
from bs4 import BeautifulSoup

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'
raw_html = requests.get('')
soup = BeautifulSoup(raw_html.content, 'html.parser')

The answer below uses urlopen, which does not produce the 406 error.

from urllib.request import Request
from urllib.request import urlopen
from bs4 import BeautifulSoup

raw_request = Request('')
raw_request.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0')
raw_request.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
resp = urlopen(raw_request)
raw_html =
soup = BeautifulSoup(raw_html, 'html.parser')

Leave a Reply

Your email address will not be published. Required fields are marked *