I have a question about Python json and urlib parsing. (Use League of Legends api)

Asked 1 years ago, Updated 1 years ago, 139 views

import urllib.request
import json


def get_encryptedID(input_name):
    apikey = "The value of the api key."
    encode_name = input_name.replace(" ", "%20")
    urls = 'https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + encode_name + '?api_key=' + apikey

    if(urllib.request.urlopen(urls).getcode() == 200):
        urls_data = urllib.request.urlopen(urls) #about opening url
        dict_data = data_process(urls_data) #urlInformation -> dict
        return dict_data['id'], dict_data['name']
    else:
        return -1, -1

def print_info(dict_data):
    print(dict_data['tier'], dict_data['rank'])
    print("win:", dict_data['wins'], "lose:", dict_data['loss'], "win:', round(win_data['wins']]/(win_data['wins'])*100, 2], "%" )

def print_unrank():
    print("Unrank")

def data_process(f):
    jsonString = f.read().decode("utf-8")
    if(len(jsonString) < 10):
        return False
    #print("jsonstring : ", jsonString)

    if(type(json.loads(jsonString)) == list):
        data = json.loads(jsonString)[0]
    elif(type(json.loads(jsonString)) == dict):
        data = json.loads(jsonString)
    #print(data)
    return data

def get_summoner_data(encryptedID):
    apikey = "The value of the api key."
    urls = 'https://kr.api.riotgames.com/lol/league/v4/entries/by-summoner/' + encryptedID + '?api_key=' + apikey
    urls_data = urllib.request.urlopen(urls) #about opening url
    dict_data = data_process(urls_data) # url info -> dict
    if(dict_data == False):
        print_unrank()
    else:
        print_info(dict_data)

def run():
    summoner_name = str(input())
    encryptedID, summoner_name = get_encryptedID(summoner_name)
    if(encryptedID == -1):
        print("Data not found - summoner not found")
    print(summoner_name)
    get_summoner_data(encryptedID)

run()

I'd like to do a quick full search using League of Legends api(https://developer.riotgames.com/).

I don't know much about Json or urlib. I've made it complicated.

It's a code that shows the total score and tier by searching the nickname, but if the nickname does not exist, the API itself sends the response code to 404 and I don't know how to make an exception.

. . .

where urlib.request.urlopen(urls).Getcode() == 200 When this is 404, an error appears and the if statement itself is blocked with an error... I don't know the solution.

And one more question, if I search for a nickname in Korean, I get an error in converting the ASCII code, so how should I solve it?

python api json urllib requests

2022-09-21 20:05

1 Answers

An HTTP error occurs and an exception must be handled with try catch.

It should be structured as below.

try:
    r = urllib.request.urlopen(urls)
except HTTPError as e:
    print(e)
else:
    # Handle if no exception occurs
    if(r.getcode() == 200):
        pass


2022-09-21 20:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.