New to Python Twitter Search API



I am trying to search for tweets with particular hashtags or keywords and save them in a list. But i am not able to access the tweets from the list itself.
The following code:
twitter = Twitter(auth = oauth)
content =’#oreo’, result_type=‘recent’, lang=‘en’, count=10)

runs smoothly but I am not able to access the data stored in content.
On running:

I get an error:
KeyError: 0

How do I store the result of my queries thruogh the API?


Which Python library are you using?


from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream

This is the import statement. Python version being 3.5.2


Thanks for getting back in touch here. It might be helpful to see a full version of the code you are working on. Do you have a full code snippet I can look at? I’ve personally had a lot of success with using the Search Tweets Python wrapper.


So how do I access the list “content”? Or how better can i store the responses from


The way that I solved this recently was to save the results of my search as stream and convert it into a list. So you would use a variable such as the following:

content = list(content)

After you have that you can use pandas to turn this into a dataframe, create a set of empty list and use a for loop to append each:

import pandas as pd

user = []
screen_name = []

for tweet in content:

df = pd.DataFrame({'user': user, 'screen_name': screen_name})

From there your data should be in a better shape to access the the first row of the data. I personally had success using the search tweets library and this method so that might be a better option for you if you are still running into blockers.


Thank you for your response.
I tried this method but it appears as the list() command isn’t working as it should. Attaching the code snippet. Also I apologise for the late reply.


Hmm, my guess is that this an issue the search tweets function of the Twitter API. Have you tried using the search tweets wrapper?


I dont have the premium credentials, guess i’ll have to make do. Thanks for your help!


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.