0

The data I am using is Twitter API's twitter trending topics.

url_0 = 'https://api.twitter.com/1.1/trends/place.json?id=2459115'  
res = requests.get(url_0, auth=auth)  

print(res, res.status_code, res.headers['content-type'])  
print(res.url)

top_trends_twitter = res.json()  
data= top_trends_twitter[0]  

This is how data looks like:

[{'as_of': '2017-02-13T21:59:32Z',  
  'created_at': '2017-02-13T21:53:22Z',  
  'locations': [{'name': 'New York', 'woeid': 2459115}],  
  'trends': [{'name': 'Victor Cruz',  
    'promoted_content': None,  
    'query': '%22Victor+Cruz%22',  
    'tweet_volume': 45690,  
    'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},  
   {'name': '#percussion',  
    'promoted_content': None,  
    'query': '%23percussion',  
    'tweet_volume': None,  
    'url': 'http://twitter.com/search?q=%23percussion'},  .....etc

Now, after I connect the server with SQL, and create database and table, an error appears. This is the part that is causing me trouble:


for entry in data:  
    trendname = entry['trends']['name']  
    url = entry['trends']['url']  
    num_tweets = entry['trends']['trend_volume']  
    date= entry['as_of']  
    print("Inserting trend", trendname, "at", url)  
    query_parameters = (trendname, url, num_tweets, date)  
    cursor.execute(query_template, query_parameters) 



con.commit()  
cursor.close()  

Then, I get this error:


TypeError                                 Traceback (most recent call last)
<ipython-input-112-da3e17aadce0> in <module>()  
     29   
     30 for entry in data:  
---> 31     trendname = entry['trends']['name']  
     32     url = entry['trends']['url']  
     33     num_tweets = entry['trends']['trend_volume']  

TypeError: string indices must be integers

How can I get the set of strings into dictionary, so that I can use that for entry data code?

1 Answer 1

2

You Need entry['trends'][0]['name']. entry['trends'] is a list and you need integer index to access items of list.

Try like so:

data=[{'as_of': '2017-02-13T21:59:32Z',  
  'created_at': '2017-02-13T21:53:22Z',  
  'locations': [{'name': 'New York', 'woeid': 2459115}],  
  'trends': [{'name': 'Victor Cruz',  
    'promoted_content': None,  
    'query': '%22Victor+Cruz%22',  
    'tweet_volume': 45690,  
    'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},  
   {'name': '#percussion',  
    'promoted_content': None,  
    'query': '%23percussion',  
    'tweet_volume': None,  
    'url': 'http://twitter.com/search?q=%23percussion'}]}]

for entry in data:
    date= entry['as_of']
    for trend in entry['trends']:
        trendname = trend['name']  
        url = trend['url']  
        num_tweets = trend['tweet_volume']  
        print trendname, url, num_tweets, date

Output:

Victor Cruz http://twitter.com/search?q=%22Victor+Cruz%22 45690 2017-02-13T21:59:32Z
#percussion http://twitter.com/search?q=%23percussion None 2017-02-13T21:59:32Z
Sign up to request clarification or add additional context in comments.

2 Comments

still getting the same issue...When i do data['as_of'], I do get a value, but when I run that code, I get the same error pointing to date= entry['as_of']
@Elizabeth The code is tested, it's working for the data you have provided in the question. I think you should try data = res.json(). It will work then I hope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.