tweepy它不会遵循某些推文

时间:2018-06-27 20:24:29

标签: python-3.x twitter tweepy

似乎有些关键字'follow'的推文 它会随之而来,对于他们中的一些人来说,它不会... 除此之外,它还可以(我没注意到其他东西) 有人可以找出问题出在哪里吗?

<div class="allpanels">
<div class="image-1-panel">1</div>
<div class="image-2-panel">2</div>
<div class="image-3-panel">3</div>
<div class="image-4-panel">4</div>
<div class="image-5-panel">5</div>
<div class="image-6-panel">6</div>
<div class="image-7-panel">7</div>
</div>

我哪里出错了?

class Listener():

def search(self, twts):
    global numoftwts
    for i in twts:
        names = ['follow', 'following']
        txt = i.text.lower()
        if not any(k in txt for k in keywords) or any(k in txt for k in bannedwords):
            continue
        if not self.is_user_bot_hunter(str(i.author.screen_name)):
            if not i.retweeted:
                try:
                    print("Trying to retweet status_id:{}".format(i.id))
                    res = api.retweet(i.id)
                    if res.retweeted:
                        api.create_favorite(i.id)
                        print('retweeted', numoftwts, 'times', '-',
                              str(datetime.datetime.fromtimestamp(time.time()).strftime('%d-%m-%Y %H:%M:%S')))

                        print(i.text)
                        print('\n')
                    else:
                        print("retweet failed")

                    if any(c in txt for c in names):
                        # print("Trying to follow something")
                        # if hasattr(i, 'retweeted_status'):
                        #     print("trying to fetch user_id")
                        user_id = i.retweeted_status.user.id_str
                        res = api.create_friendship(user_id)
                        res = api.get_user(user_id)
                        if res.following:
                            print("Successfully followed :{}".format(user_id))
                            print('\n')

                except Exception as e:
                    print("Exception:".format(str(e)))
                    continue
        sleep(600)

     def run(self):
            for eachkey in keywords:
            tweets = api.search(q=eachkey, result_type='mixed', lang='en')
            self.search(tweets)

if __name__ == '__main__':
    while True:
        r = Listener()
        r.run()

2 个答案:

答案 0 :(得分:0)

AttributeError: 'Status' object has no attribute 'retweeted_status'
-> user_id = i.retweeted_status.user.id_str

这意味着您要获得非转发的推文的用户ID。

我想知道一条推文是否是RT,测试是:

if hasattr(tweet, 'retweeted_status'):
    # this tweet is a RT

答案 1 :(得分:0)

如果遇到无法从特定用户获取推文的错误,请使用:

try:
    specific_tweets = tweepy.Cursor(api.search, tweet_mode='extended', q= <some query>, lang='en').items(500)
except tweepy.error.TweepError:
    pass

如果您要访问tweet的转推属性,请执行以下操作:

if hasattr(tweet, 'retweeted_status'):
            extracted_author = tweet.retweeted_status.user.screen_name
        else: extracted_author = tweet.user.screen_name

基本上检查一条推文的hasattr(tweet, 'retweeted_status')是否正确。它检查该推文是否具有名为“ retweeted_status”的属性