samedi 9 mai 2015

How would I organize the dicts into a list using this websites API?

For a project I am using this websites API:

http://ift.tt/1KsnbZf

I am having difficulty separating the dicts into a list. This is my code for the project so far:

import json

import urllib.request

import statistics

    def get_website_stats(website):
    url = 'http://ift.tt/1zSi44A'+ website +'&output=json'
    lines = urllib.request.urlopen(url).readlines()
    list_of_strings = []
    for obj in lines:
        list_of_strings.append(obj.decode('utf-8').strip('\n'))
    merged_string = ' '
    for string in list_of_strings:
        if string not in ('[', ']'):
            merged_string += string
    return json.loads(merged_string)

    symbol=input("What is the website name? (Do not include www)")
    stats = get_website_stats(symbol)
    print(stats['bro'])

If you read through the API it is obvious I am trying to print out the browser the user of the website is using but instead I get an error:

TypeError: list indices must be integers, not str

Could anyone help me organize the dicts into the list properly? I think the problem is I am appending an entire line into each element of the list.

Aucun commentaire:

Enregistrer un commentaire