Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Twitter API-如何通过用户获取实时推文_Python_Twitter_Tweepy - Fatal编程技术网

Python Twitter API-如何通过用户获取实时推文

Python Twitter API-如何通过用户获取实时推文,python,twitter,tweepy,Python,Twitter,Tweepy,有没有一种通过Twitter句柄或Twitter ID获取实时推文的方法 我尝试过使用,但只能通过关键字过滤推文: import tweepy import json # Import the necessary methods from tweepy library from tweepy.streaming import StreamListener from tweepy import OAuthHandler from tweepy import Stream # Variables

有没有一种通过Twitter句柄或Twitter ID获取实时推文的方法

我尝试过使用,但只能通过关键字过滤推文:

import tweepy
import json

# Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Variables that contains the user credentials to access Twitter API 
access_token = "INSERT ACCESS TOKEN"
access_token_secret = "INSERT ACCESS TOKEN SECRET"
consumer_key = "INSERT CONSUMER KEY"
consumer_secret = "INSERT CONSUMER SECRET"

# This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status


if __name__ == '__main__':
    # This handles Twitter authentication and connection to Twitter Streaming API
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, l)
    stream.filter(track=['european central bank'])

我是否可以通过Twitter用户名下拉推文?

您应该可以这样传递关键字:

stream.filter(follow=['user_id'])
它接收一个用户ID列表

从文档中,应返回:

  • 用户创建的推文
  • 用户转发的推文
  • 回复用户创建的任何推文
  • 用户创建的任何推文的转发
  • 手动回复,不按回复按钮创建(例如“@twitterapi I agree”)
它不会返回:

  • 提及用户的推文(例如,“Hello@twitterapi!”)
  • 无需按下Retweet按钮即可手动创建Retweet(例如,“RT@twitterapi API非常棒”)
  • 受保护用户的推文
注意

您可以指定
track
follow
两个值,它将生成带有输入关键字的tweet或由输入用户创建的tweet

嘿,福吉,有没有办法利用受保护的用户?如何只获取原始用户创建的推文,而不获取其他内容(转发等)?