Use Tweepy to learn tweets containing certain strings at regular intervals and tweet the sentences contained in those tweets

Asked 2 years ago, Updated 2 years ago, 74 views

Currently, I am having trouble creating a twitterbot on Tweepy.
I'd like to write a program where I learn tweets containing certain strings at regular intervals and tweet the sentences contained in those tweets by myself instead of RT.

got exception:
'SearchResults' object has no attribute' text'

If the text does not exist, it will appear.Is it impossible to take out a sentence and tweet the content?
The source code is as follows

#coding:UTF-8
import tweet
import time

while True:


    CONSUMER_KEY = '**********'
    CONSUMER_SECRET = '********************'
    auth=tweepy.OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
    ACCESS_TOKEN='**************'
    ACCESS_SECRET = '********************'
    auth.set_access_token(ACCESS_TOKEN,ACCESS_SECRET)

    api=tweepy.API(auth)

    search=api.search(q='#paperplane', lang='ja', count=10)
    try:
        api.update_status(search.text)
    exceptAttributeError as:
        print("got exception:")
        print(e)

    time.sleep(30)

python twitter

2022-09-30 14:11

2 Answers

http://docs.tweepy.org/en/v3.5.0/api.html?highlight=search

Help Methods API.search(q[,lang][,locale][,rpp][,page][,
since_id][,geocode][,show_user]) Returns tweets that match a
specified query.

Return type:list of SearchResult objects

The return value of the API.search method in tweet seems to return a list of SearchResult objects.
So I think you can do it like this.

search=api.search(q='#paperplane',lang='ja',count=10)
 For in search:
     api.update_status(s.text)


2022-09-30 14:11

Now you know the tweet and the username that tweeted it

search=api.search(q='#a',lang='ja',count=2)
For in search:
    print(s.author._json['screen_name'])
    print(s.text)


2022-09-30 14:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.