New to Python Twitter Search API

python
search

#1

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 = twitter.search.tweets(q=’#oreo’, result_type=‘recent’, lang=‘en’, count=10)

runs smoothly but I am not able to access the data stored in content.
On running:
content[0][‘user’][‘screen_name’]

I get an error:
KeyError: 0

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


#2

Which Python library are you using?


#3

from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream

This is the import statement. Python version being 3.5.2


#4

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.


#5

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


#6

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:
    user.append(content['user'])
    user.append(content['screen_name'])


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

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.


#7

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.


#8

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


#9

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


#10

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