python urlopen returns error

Question:

I am trying to parse some data from ‘https://datausa.io/profile/geo/jacksonville-fl/#intro‘, but I am not sure how to access it from python. My code is:

adress, headers = urllib.request.urlretrieve('  https://datausa.io/profile/geo/jacksonville-fl/#intro')
handle = open(adress)

and it returns the error:

Traceback (most recent call last):
  File "C:/Users/Jared/AppData/Local/Programs/Python/Python36-32/capstone1.py", line 16, in <module>
    adress, headers = urllib.request.urlretrieve('  https://datausa.io/profile/geo/jacksonville-fl/#intro')
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 248, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 532, in open
    response = meth(req, response)
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 642, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 570, in error
    return self._call_chain(*args)
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 504, in _call_chain
    result = func(*args)
  File "C:UsersJaredAppDataLocalProgramsPythonPython36-32liburllibrequest.py", line 650, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

Please explain what is wrong or tell me a better way to access the page. Also, does the ‘ .io ‘ suffix affecthow python handles it?
Thanks.

Asked By: Inexorable

||

Answers:

This worked for me:

import requests
url = "https://datausa.io/profile/geo/jacksonville-fl/#intro"
req = requests.request("GET",url)
Answered By: Paula Thomas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.