JSON Format Inside a Link to Format

Viewed 28

so I'm wondering if there's anyway I can make a more suitable code for printing a certain type of value from a json file.

Code:

try:
    import requests
    from bs4 import BeautifulSoup as bs
    import os
except ImportError as ex:
    input(f"Module {ex.name} not installed, to install run '{'python' if os.name == 'nt' else 'python3.8'} -m pip install {ex.name}'\nPress enter to exit")
    exit()
def main():
    os.system('cls' if os.name == 'nt' else 'clear')
    print ("Input the userID below!")
    userID =str(input('~ '))
    os.system('cls' if os.name == 'nt' else 'clear')
    URL = "https://verify.eryn.io/api/user/"+userID
    req = requests.get(URL)
    soup = bs(req.text, 'html.parser')
    print(soup)
    os.system("pause")
    main()
main()

Output(s):

{
  "status": "ok",
  "robloxUsername": "USERNAME%20HERE",
  "robloxId": USER%20ID%20HERE
}

I'm wondering how can I print the status username and id.

1 Answers

Okay, found a solution. Since I want to print the json variable(s), I have to use the packages: json and urllib.request json is the main and urllib helps make the process work, lemme show you with code though.

try:
    import urllib.request
    import json
    import os
except ImportError as ex:
    input(f"Module {ex.name} not installed, to install run '{'python' if os.name == 'nt' else 'python3.8'} -m pip install {ex.name}'\nPress enter to exit")
    exit()
def main():
    os.system('cls' if os.name == 'nt' else 'clear')
    print ("Input the userID below!")
    userID =str(input('~ '))
    os.system('cls' if os.name == 'nt' else 'clear')
    URL = "https://verify.eryn.io/api/user/"+userID
    req = urllib.request.urlopen(URL)
    js = json.loads(req.read().decode())
    print(js["robloxUsername"])
    os.system("pause")
    main()
main()
Related