Home > Back-end >  Import a pre-made list from a web page in python
Import a pre-made list from a web page in python

Time:09-23

Since I'm a beginner, I need advice from professionals.

I want to import the list from the url: http://45.77.228.203:3001/V2/battlesruleset?ruleset=Standard&mana=12&player=tyaaz&token=undefined&summoners=[5,16,27,38,49,167,178,189,145,156,145,189,224,259]

So i tried it with the code:

import requests
    
card_deck = list(requests.get("http://45.77.228.203:3001/V2/battlesruleset?ruleset=Standard&mana=12&player=tyaaz&token=undefined&summoners=[5,16,27,38,49,167,178,189,145,156,145,189,224,259]"))
    
print(card_deck[0])

Output:

b'[{"summoner_id":"16","monster_1_id":"169","monster_2_id":"131","monster_3_id":"98","monster_4_id":"172","monster_5_id":"","monst'

The list seems to have slipped a bit. My desired output would have been something like this:

{"summoner_id": "16", "monster_1_id": "169", "monster_2_id": "131", "monster_3_id": "98", "monster_4_id": "172" , "monster_5_id": "", "monster_6_id": "", "ruleset": "Standard", "mana_cap": "12", "tot": "31", "ratio": "96.77419354838709677400"}

CodePudding user response:

Like Chris commented, the response is in JSON format.
The code below handles json->python list conversion of the response:

import requests
from json import loads

card_deck_request = requests.get("http://45.77.228.203:3001/V2/battlesruleset?ruleset=Standard&mana=12&player=tyaaz&token=undefined&summoners=[5,16,27,38,49,167,178,189,145,156,145,189,224,259]")
card_deck_list = loads(card_deck_request.text)
card_deck_list[0]

outputs

{'summoner_id': '16', 'monster_1_id': '169', 'monster_2_id': '131', 'monster_3_id': '98', 'monster_4_id': '172', 'monster_5_id': '', 'monster_6_id': '', 'ruleset': 'Standard', 'mana_cap': '12', 'tot': '31', 'ratio': '96.77419354838709677400'}
  • Related