简体   繁体   中英

Can't retweet with tweepy - Python

i'm trying to create a bot to retweet #dogs. This is my code

    import tweepy

consumer_key = 'XXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXX'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

user = api.me()
print(user.name)

def main():
    search = ('#dogs')
    for tweet in tweepy.Cursor(api.search, search).items(1):
        try:
            tweet.retweet()
            print('Tweet Retweeted')
        except tweepy.TweepError as e:
            print(e.reason)
        except StopIteration:
            break

main()

When i run it, i get my username from (user.name) but it never sends the retweet.

CMD output:

C:\Users\xd\Desktop>python rt_bot.py
Dom

api.retweet(tweet) instead of tweet.retweet() should probably do it.

For more reference: http://docs.tweepy.org/en/v3.5.0/api.html?highlight=retweet

EDIT: From my comment - keep tweet.retweet() but remove the 1 from tweepy.Cursor(api.search, search).items() , so that you search through all tweets rather than just the first tweet you can.

The parameter of items() determines how many tweets you search through, and leaving it blank will allow you to search through as many as tweepy allows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM