Bottom Page

Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading json from webpage
#1
For starters I'm just trying to learn how to pull some json data off a siriusxm page. (( Here's a link, but the time stamped page doesn't last very long. siriusxm page

My code looks like it grabs all the data fine, but I need assistance on how to pull specific data from the json.
import urllib3
import json


http = urllib3.PoolManager()

r = http.request('GET', 'https://www.siriusxm.com/metadata/pdt/en-us/json/channels/firstwave/timestamp/01-10-18:00:00')

print(json.loads(r.data.decode('utf-8'))['channelMetadataResponse'])
for example, the json.loads(r.data.decode('utf-8'))['channelMetadataResponse'] has the data just fine, but changing the syntax to
json.loads(r.data.decode('utf-8'))['artists':'name'] gives a slicing error.

Direction would be grateful. Thank you so much!

In case the link dies or is blank. here is a sample from their page. you may need copy paste into http://jsonviewer.stack.hu to read it better.

{"channelMetadataResponse":{"messages":{"code":100,"message":"Successful request"},"status":1,"metaData":{"channelId":"firstwave","channelName":"1st Wave","channelNumber":33,"currentEvent":{"artists":{"id":"8_l","name":"Talking Heads"},"baseUrl":"http:\/\/albumart.siriusxm.com\/albumart\/","keyIndex":"null","siriusXMId":568746322,"song":{"album":{"name":"Little Creatures"},"composer":"","creativeArts":[{"encrypted":false,"size":"THUMBNAIL","type":"IMAGE","url":"1730\/NDCA-000017418-003_t.jpg"},{"encrypted":false,"size":"SMALL","type":"IMAGE","url":"1730\/NDCA-000017418-003_s.jpg"},{"encrypted":false,"size":"MEDIUM","type":"IMAGE","url":"1730\/NDCA-000017418-003_m.jpg"},{"encrypted":false,"size":"LARGE","type":"IMAGE","url":""},{"encrypted":false,"type":"BIO","url":""},{"encrypted":false,"type":"REVIEWS","url":""},{"encrypted":true,"size":"THUMBNAIL","type":"IMAGE","url":"1730\/NDCA-000017418-003_t.jpg"},{"encrypted":true,"size":"SMALL","type":"IMAGE","url":"1730\/NDCA-000017418-003_s.jpg"},{"encrypted":true,"size":"MEDIUM","type":"IMAGE","url":"1730\/NDCA-000017418-003_m.jpg"},{"encrypted":true,"size":"LARGE","type":"IMAGE","url":""},{"encrypted":true,"type":"BIO","url":""},{"encrypted":true,"type":"REVIEWS","url":""}],"id":"$O4GA","name":"Stay Up Late"},"startTime":"2019-01-10T17:46:51Z"},"dateTime":"2019-01-10T17:49:52.311Z","version":1.1}}}
Quote
#2
I believe the problem is your slice. To my knowledge, you cannot do ["artists":"name"]. That should be a dict retrieval key of ["artists"] to return the sub-dict or list contained in "artists".

That said, what is the error you're receiving.
jmair likes this post
Quote
#3
The error is
TypeError: unhashable type: 'slice'

So you're spot on =)
Quote
#4
data = json.loads(r.data.decode('utf-8'))
artist_name = data['channelMetadataResponse']['currentEvent']['artist']['name']
by the way, look at requests - you can directly get response as json
http://docs.python-requests.org/en/maste...se-content
stullis and jmair like this post
Quote
#5
Perfect, now I see how to call it. I appreciate the hint.

artist_name = data['channelMetadataResponse']['metaData']['currentEvent']['artists']['name']
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Database Creation and Reading Tussocky_Coder 3 79 Jan-15-2019, 02:20 AM
Last Post: Larz60+
  Reading file names CertifiedPengu 1 105 Jan-07-2019, 06:54 PM
Last Post: Gribouillis
  Problem with reading a path gkiller007 30 490 Jan-05-2019, 10:09 PM
Last Post: snippsat
  Issue with reading CSV file nnsatpute 12 240 Dec-19-2018, 12:08 PM
Last Post: Gribouillis
  reading from 2 pipes Skaperen 2 234 Oct-05-2018, 04:45 AM
Last Post: Skaperen
  reading csv and writing csv chawas 2 256 Aug-23-2018, 09:28 AM
Last Post: chawas
  Reading a html file peterl 4 321 Aug-20-2018, 03:16 PM
Last Post: peterl
  HTMLParser Reading even the Closing Tag soothsayerpg 6 516 Aug-02-2018, 07:34 AM
Last Post: soothsayerpg
  Multiple input box in a webpage have same XPATH sumandas89 3 399 Jul-19-2018, 08:41 AM
Last Post: buran
  Getting error when accessing elements in a modal window of an webpage using selenium sumandas89 3 713 Jul-13-2018, 10:44 AM
Last Post: mlieqo

Forum Jump:


Users browsing this thread: 1 Guest(s)