Search code examples
pythonjsonpython-requestspypi

403 error when trying to make requests to pypi api


My code:

import requests

x = requests.get('http://pypi.python.org/pypi/urllib3/json')

print(x)

Output:

<Response [403]>

Why am I getting this response even though it works fine in the browser?

I tried to print the json:

import requests

x = requests.get('http://pypi.python.org/pypi/urllib3/json')

print(x.json())

But it gave me an error:

Traceback (most recent call last):
  File "C:\Users\HP\Desktop\temp.py", line 5, in <module>
    print(x.json())
  File "C:\Users\HP\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Solution

  • It should be https and not http.

    This will work.

    import requests
    
    x = requests.get('https://pypi.python.org/pypi/urllib3/json')
    print(x)
    
    json_str = x.json()
    print(json_str)
    
    <Response [200]>
    
    {'info': {'author': 'Andrey Petrov', 'author_email': '[email protected]', 'bugtrack_url': None,....}