[英]Tweepy can't find Track
I need get tweets for my app, but tweepy can't find some tracks. 我需要为我的应用获取推文,但是tweepy找不到一些音轨。 I've searched for any site and can't find why is happening.There is my code.
我搜索了任何网站,找不到发生的原因。这是我的代码。 Some ideas?
有什么想法吗?
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'])
I found a better soluction with api.cursor: 我发现使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.