Tweepy Why did I receive AttributeError for search

Viewed 22065

I received the following error message when I ran the code. The code should return the tweets that match the specified query. Did Twitter block the search attribute?

Error Message

for tweets in api.search(q="iphone", lang="en"):
AttributeError: 'API' object has no attribute 'search'

Code:

import tweepy
consumer_key = "XX"
consumer_secret = "XX"
access_token = "XX"
access_token_secret = "X"

# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Setting your access token and secret
auth.set_access_token(access_token, access_token_secret)
# Creating the API object while passing in auth information
# Creating the API object while passing in auth information
# Creating the API object while passing in auth information


api = tweepy.API(auth)

for tweets in api.search(q="iphone", lang="en"):
    print(tweet.text)
2 Answers

Yes! search is not a valid attribute. According to the docs, you should use search_tweets.

Docs are here.

for tweets in api.search_tweets(q="iphone", lang="en"):
    print(tweet.text)

Tweepy v4.0.0 was released yesterday and renamed API.search to API.search_tweets.

I would recommend simply renaming your method call, but you could also downgrade to Tweepy v3.10.0.

Related