Home > Enterprise >  Twitter API throws error : 'API' object has no attribute 'search'
Twitter API throws error : 'API' object has no attribute 'search'

Time:02-11

The following code I wrote was intended to retweet tweets with #programming. But, anything I run the code I get an error "search" object is not an attribute of the Twitter API. The error is posted below the code. Thanks

import tweepy
import time 

# get api code and password(secret)
comsumers_key = '#########'
comsumers_secret = '######'

token_key = '#########'
token_secret = '###########'


auth = tweepy.OAuthHandler(comsumers_key,comsumers_secret)
auth.set_access_token(token_key, token_secret)

api = tweepy.API(auth)

hashtag = "programming"
tweetNum = 20
tweets = tweepy.Cursor(api.search, hashtag).items(tweetNum)


def bot1():
    for tweet in tweets:
        try:
            tweet.retweet()
            print("retweet")
            time.sleep(50)
        except tweepy.TweepError as e:
            print(e.reason)
            time.strftime(20)
bot1()

error:

Traceback (most recent call last):
  File "/Users/sonter/tweetbot/bot1.py", line 48, in <module>
    tweets = tweepy.Cursor(api.search, hashtag).items(tweetNum)
AttributeError: 'API' object has no attribute 'search'

CodePudding user response:

The Cursor expects a regular api method, but looking at its reference doc there is no search only, but :

  • search_30_day
  • search_full_archive
  • search_tweets
  • search_users
  • search_geo

Maybe you meant one of them ?

  • Related