Home > database >  Python: Tweepy Comment Picker no attribute comments
Python: Tweepy Comment Picker no attribute comments

Time:01-03

I wrote a small python script to pick a random comment under a tweet, but can't find a way around the error.

import random
import tweepy


consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'

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


tweet_id = "xxx"  # zahlen mit tweet id ersetzten
comments = api.comments(tweet_id)


followed_user = "xxx"  # Ersetzen Sie durch das Handle des Benutzers, nach dem Sie filtern moechten
filtered_comments = [c for c in comments if c.user.screen_name == followed_user]

if filtered_comments:
    winner = random.choice(filtered_comments)
    print(f"Der Gewinner ist @{winner.user.screen_name} mit dem Kommentar: {winner.text}")
else:
    print("Keine Kommentare Gefunden.") 

That is the error: AttributeError: 'API' object has no attribute 'comments' when i tried it other ways i get errors: AttributeError: 'API' object has no attribute 'search', and that i need evaluated api access

CodePudding user response:

The methods you are using seem not to exist in the API object. You can check tweepy's documentation to see available methods.

The API.search method was renamed to API.search_tweets in version 4.0 so you might want to check your tweepy version.

To solve your problem you can try this:

import random
import tweepy

consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'

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

tweet_id = "xxx"  # zahlen mit tweet id ersetzten
tweet = api.get_status(tweet_id, tweet_mode='extended')

comments = []
for page in tweepy.Cursor(api.search_tweets, q='to:{}'.format(tweet.user.screen_name), in_reply_to_status_id=tweet.id, tweet_mode='extended').pages(100):
    comments.extend(page)

followed_user = "xxx" # Ersetzen Sie durch das Handle des Benutzers, nach dem Sie filtern moechten
filtered_comments = [c for c in comments if c.user.screen_name == followed_user]

if filtered_comments:
    winner = random.choice(filtered_comments)
    print(f"Der Gewinner ist @{winner.user.screen_name} mit dem Kommentar: {winner.text}")
else:
    print("Keine Kommentare Gefunden.")
````
  • Related