Tweepy找不到Track

时间:2014-11-14 22:16:54

标签: python twitter tweepy

我需要为我的应用获取推文,但是tweepy无法找到一些曲目。我搜索过任何网站,无法找到原因。我的代码就是。一些想法?

class listener(StreamListener):
 def __init__(self, api=None):
    self.api = api or API()
    self.n = 0
    self.m = 100

 def on_data(self, data):
           self.n = self.n+1
           if self.n < self.m:
                lang = data.split('","lang":"')[1].split('","')
                lang = lang[0][0:2]
                #if(guessLanguageName(text_tweet) == "English"):
                if(lang  == "en"):
                     tweet = data.split('"id_str":"')[1].split('","source":"')[0]
                     text_tweet = tweet.split('","text":"')[1]
                     id_tweet = tweet.split('","text":"')[0]
                     out = id_tweet +' '+ text_tweet

                     print out + "\n"
                     tweets = open('tweets.txt', 'a')
                     tweets.write(out)
                     tweets.write('\n')
                     tweets.close()
                     return True
           else:
             return False

 def on_error(self, status):
    print status

auth = tweepy.OAuthHandler(ckey, csecret)    
auth.set_access_token(atoken, asecret)    
twitterStream = tweepy.streaming.Stream(auth, listener())      
archive = open('hashtag.txt', 'r')    
twitterStream.filter(track=['sony xperia u'])    

1 个答案:

答案 0 :(得分:1)

我找到了一个更好的api.cursor解决方案:

import tweepy
def twitter_fetch(search_name,maxnumtweets):
    consumer_token = ''
    consumer_secret = ''
    access_token = ''
    access_secret = ''

    auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
    auth.set_access_token(access_token,access_secret)

    api  = tweepy.API(auth)
    count = 0
    count_url = 0
    count_url_total = 0
    count_tweets = 0

    try:
       for tweet in tweepy.Cursor(api.search,q=(search_name), lang = 'en').items(maxnumtweets):
          count_url =  len(tweet.entities['urls'])
          count_url_total += count_url
          count +=1
          if count_url > 0:
                        count_check_text = 0
                        text = tweet.text

                        print "urls: "
                        for url in tweet.entities['urls']:
                            print url['url']
                            count_check_text += 1               

                            no_expanded_urls.write(url['url'].encode('utf-8'))
                            no_expanded_urls.write('\n')

                        out = tweet.id_str +" " + text
                        print "\nid_str and tweet: "
                        print out.encode('utf-8')

                        tweets.write(out.encode('utf-8'))
                        tweets.write('\n')
                        count_tweets+=1
                        print "-------------------------------------------------------------"
       print "-------------------------------------------------------------"
       print "-------------------------------------------------------------"
       tweets.close()
       no_expanded_urls.close()
       print "Searched for: " + hashtag
       print "Count tweets:"
       print count_tweets
       print "Count urls:"
       print count_url_total
       print count

    except tweepy.TweepError as e:
        print "error"

if __name__ == '__main__':
     archive = open('hashtag.txt', 'r')
     hashtag = archive.readline()
     archive.close()
     count_tweets = 0
     max_tweets = 50

     tweets = open('tweets.txt', 'a')
     tweets = open('tweets.txt', 'w')
     no_expanded_urls = open('noExpandedUrls.txt', 'w')
     twitter_fetch(hashtag, max_tweets)