Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16921

How to get image url string from j["data"] in Python?

$
0
0

I have the following snippet of code that creates json data (?dictionary?). I can't figure out how to get the avatarUrl string

   def __init__(self, character_id):        self.character_id = character_id        self.character_data = self.get_character_data()        print(self.character_data["id"])    def update(self):        self.character_data = self.get_character_data()    def get_character_data(self):        req = requests.get(f"https://character-service.dndbeyond.com/character/v5/character/{self.character_id}")        #print("hello")        if req.status_code != 200:            return None        try:            j = req.json()            if not "success" in j or not "data" in j:                return None            return j["data"]        except ValueError:            return None

The resulting data looks like this. I'm trying to catch the url for {'avatarUrl': 'https://www.dndbeyond.com/avatars/21222/111/637708177497566513.jpeg'}, so I can open the image.

Press CTRL+C to quit
{'id': 108291017, 'userId': 118256620, 'username': 'sethirya', 'isAssignedToPlayer': True, 'readonlyUrl': 'https://dndbeyond.com/characters/108291017', 'decorations': **{'avatarUrl': 'https://www.dndbeyond.com/avatars/21222/111/637708177497566513.jpeg?**width=150&height=150&fit=crop&quality=95&auto=webp', 'frameAvatarUrl': 'https://www.dndbeyond.com/avatars/7169/957/637042612736861450.png', 'backdropAvatarUrl': 'frameAvatarDecorationKey': '558', 'frameAvatarId': 7169957, 'backdropAvatarDecorationKey': '407', 'backdropAvatarId': 61586, 'smallBackdropAvatarDecorationKey': '61587', 'smallBackdropAvatarId': 61587, 'largeBackdropAvatarDecorationKey': '61588', 'largeBackdropAvatarId': 61588, 'thumbnailBackdropAvatarDecorationKey': '61589', 'thumbnailBackdropAvatarId': 61589, 'themeColor': {'themeColorId': 429, 'themeColor': '#e5623e', 'backgroundColor': '#FEFEFE', 'name': 'Barbarian Fire', 'raceId': None, 'subRaceId': None, 'classId': 9, 'tags': ['Class Themes'], 'decorationKey': '429'}}, 'name': 'Raine', 'socialName': None, 'gender': 'Female/Non-binary', 'faith': '', 'age': 29, 'hair': '', 'eyes': 'gr

I tried just using the print function to obtain the desired item. I can print many other items, but I need help with this.


Viewing all articles
Browse latest Browse all 16921

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>