How to use the tweepy's search API?


#1

When I’m crawling the tweets, I used the Tweepy.
I tried to search some tweets when they were written past periods (e.g. 2014.7.1 to 2014.7.31)
My python code is below.

    from tweepy import Stream
    from tweepy import OAuthHandler
    from tweepy.streaming import StreamListener
    import tweepy
    import time

    ckey ='x'
    csecret = 'x'
    atoken = 'x'
    asecret ='x'

    auth = tweepy.OAuthHandler(ckey, csecret)
    auth.set_access_token(atoken, asecret)

    api = tweepy.API(auth)

    for tweet in tweepy.Cursor(api.search,q='test',since='2014-07-01',until='2014-07-31').items():
        print tweet.text

However, after compiling, I got this errors

    Traceback (most recent call last):
      File "D:\workplace\twitter_crawler\src\twitter_crawler_main.py", line 98, in <module>
        for tweet in tweepy.Cursor(api.search,q='name', since='2014-07-01',until='2014-07-31').items():
      File "build\bdist.win32\egg\tweepy\cursor.py", line 181, in next
      File "build\bdist.win32\egg\tweepy\cursor.py", line 101, in next
    AttributeError: 'function' object has no attribute '__self__'

How can I fix the AttributeErrors, could you help me my problems? Thanks for reading.


#2

[I moved this post from the Streaming category to REST, since you’re not actually using the Streaming API]

Your code works fine for me (although I changed the dates to August (08) from July, as the Search index doesn’t go back further than 7-10 days).

What version of Python are you using? Tweepy doesn’t currently work fully on Python 3. There’s a forum for Tweepy run by the developers at http://discuss.tweepy.org/ which may be helpful for you.


#3

Is there any method to collect tweets that are older than that time window? I’d like to collect tweets from the beginning of June 2014, that’s why I was wondering.


#4

There’s no public search API that can help with that, I’m afraid - actually the search API has always been limited to a shorter period of time.

I realise that this may not be the answer you’re looking for, but we do offer the commercial Gnip API which enables search over the whole of the Twitter archive and with much greater granularity.


#5

Hi,

Is there a way to add a particular time to the search too?
Like:

for tweet in tweepy.Cursor(api.search,q='test',since='2015-06-26 10:01:25',until='2015-06-26 10:15:10').items():

Would really like the help :slight_smile:


#6

You can do hourly search by using since_id and max_id. since_id represents starting and max_id represents ending. See this tutorial http://coding2day.com/TwitterPython.pdf