Python Twitter过滤器和收集推文

Python Twitter过滤器和收集推文,python,api,twitter,tweepy,Python,Api,Twitter,Tweepy,我现在正在从事一个自然语言处理项目,但我在一开始就被困在收集特定语言的tweet上 我试图将tweepy库与python结合使用,但这段代码在控制台上没有给出任何返回 我哪里做错了 from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener import time import json # authentication data- get this

我现在正在从事一个自然语言处理项目,但我在一开始就被困在收集特定语言的tweet上

我试图将tweepy库与python结合使用,但这段代码在控制台上没有给出任何返回

我哪里做错了

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json

# authentication data- get this info from twitter after you create your application
ckey="k6Lqgu45T6ReNFO7OlnKc9zeY"
csecret="hkB5xEApV8fzdhlRGGw35VYqj1AereBriZZgHlf9r0V23NOqY8"
atoken="74417799-Rv6hCRyr1lyv14FCrgIac97AlLy0eSpd0s4hqFx23"
asecret="D0j5HNB1ec4POzxZZemjJ4CvZ0WMcLAK4D0e46r7DaPzF"

# define listener class
class listener(StreamListener):

    def on_data(self, data):
        try:
            print (data)   # write the whole tweet to terminal
            return True
        except BaseException as e:
            print('failed on data, ', str(e)) # if there is an error, show what it is
            time.sleep(5)  # one error could be that you're rate-limited; this will cause the script to pause for 5 seconds

    def on_error(self, status):
        print (status)

# authenticate yourself
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(languages=['tr'])  # track what you want to search for!

我运行了您的代码,收到一个406错误,这意味着该查询不是可接受的请求。在过滤器方法中添加轨迹项参数后,效果良好。我认为这是API本身的一个限制。另请参见。

例如,我想搜索10000条包含“网球”一词的推文,并打印推文文本和作者

api = tweepy.API(auth)
TestTweet = tweepy.Cursor(api.search, q="#tennis").items(10000)

while True:
  try:
      tweet = TestTweet.next()
      print(str(tweet.author.screen_name))
      print(tweet.text)

 except tweepy.error.TweepError:
      print "Twitter rate limit, need to wait 15 min"
      time.sleep(60 * 16)
      continue
 except StopIteration:
      break
如果你想按用户名搜索

tweet = api.get_status(id=user_name)
test_text = tweet.text
test_user = tweet.user.screen_name

即使使用track参数,它也不会打印请求的任何结果。我不知道如何首先打印。我会从您的初始问题中删除您的API密钥。第二,我用我的凭证运行了你的代码,它运行得很好。是否有任何错误代码为您打印?它甚至不会给出任何响应,即使带有轨迹参数。它只是在工作,甚至没有调试。请尝试生成新的API键。作为建议,不要在线发布键,即使它们是模拟键,因为有人可以使用这些键来模拟您