-2

I'm testing an API from Rapidapi.com using Python 3.8 and Requests module. This is the code:

import requests

url = "https://netflix-unofficial.p.rapidapi.com/api/genres"

headers = {
    'x-rapidapi-host': "netflix-unofficial.p.rapidapi.com",
    'x-rapidapi-key': //here goes my private key
}

response = requests.request("GET", url, headers=headers)

data = response.text

print(data)

The output I obtain is like this:

{"data":["Action & Adventure","Anime Features","Children & Family Movies","Classic Movies","Cult Movies","Documentaries","Dramas","Faith & Spirituality","Horror Movies","Independent Movies","International Movies","LGBTQ Movies","Movies","Music & Musicals","Romantic Movies","Sci-Fi & Fantasy","Sports Movies","Stand-Up Comedy","Thrillers"]}

But I want have access to every single genre listed and show it like this instead:

- Genre 1
- Genre 2
...
- Genre n 
K0sm
  • 47
  • 8

3 Answers3

1

It seems that the genres are stored in the variable data as single elements in an array. You could run a loop for the length of the data variable and then print the individual elements:

print(data[i])
ArkenSmaug
  • 11
  • 1
  • I've already tried this but it takes the element as a single character of the entire string. Data is not treated as a variable because of .text method in requests module. – K0sm May 17 '20 at 09:12
1

Your data is actually JSON. You can convert this string to a Python object and do whatever you want with it afterwards:

import json

data = '{"data":["Action & Adventure","Anime Features","Children & Family Movies","Classic Movies","Cult Movies","Documentaries","Dramas","Faith & Spirituality","Horror Movies","Independent Movies","International Movies","LGBTQ Movies","Movies","Music & Musicals","Romantic Movies","Sci-Fi & Fantasy","Sports Movies","Stand-Up Comedy","Thrillers"]}'

genres = json.loads(data)['data']
# ['Action & Adventure', 'Anime Features', 'Children & Family Movies', ...]

Print it one by line:

print('\n'.join(genres))

Or if you want an hyphen:

print('\n'.join([f'-{genre}' for genre in genres]))

Output:

-Action & Adventure
-Anime Features
-Children & Family Movies
-Classic Movies
...

You can directly use requests to manipulate the JSON for you:

import requests

url = "https://netflix-unofficial.p.rapidapi.com/api/genres"

headers = {
    'x-rapidapi-host': "netflix-unofficial.p.rapidapi.com",
    'x-rapidapi-key': //here goes my private key
}

response = requests.request("GET", url, headers=headers)

data = response.json()
genres = data['data']
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

Your string as er below.

Gene_Dict = '{"data":["Action & Adventure","Anime Features","Children & Family Movies","Classic Movies","Cult Movies","Documentaries","Dramas","Faith & Spirituality","Horror Movies","Independent Movies","International Movies","LGBTQ Movies","Movies","Music & Musicals","Romantic Movies","Sci-Fi & Fantasy","Sports Movies","Stand-Up Comedy","Thrillers"]}'
Gene_Dict = Gene_Dict.strip()     
Gene_Dict = eval(Gene_Dict)

for ele in Gene_Dict[Gene_Dict.keys()[0]]: print ele
OO7
  • 350
  • 2
  • 12
  • Error: 'str' object has no attribute 'keys' – K0sm May 17 '20 at 09:18
  • Added few line in the code to convert string into dictionary. – OO7 May 17 '20 at 09:57
  • Don't use `eval`, that's very bad practice. The data is JSON, one should use JSON. Also, `Gene_Dict.keys()[0]` is unnecessary and dangerous, because 1/ the key is well known, it's `'data'` and 2/ if there were more than one key, there's no guarantee that you would get the right one. Also, the output is not formatted as expected. – Thierry Lathuille May 17 '20 at 10:30
  • You can have a look at [this question](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) about why not to use eval. – Thierry Lathuille May 17 '20 at 10:42