使用我在此处找到的 tweepy 流式传输示例,您可以编辑过滤器以搜索“@yourhandle”。
您可以通过添加以下内容来访问发推文的人:
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.text
print status.user.screen_name #<----------------THIS LINE
会告诉你是谁发了你的推文。您接下来可以尝试使用以下方法更新状态:
api.update_status("my update", in_reply_to_status_id = tweetid)
可以使用以下方式获取推文 ID:
status.id
代码应如下所示:
import tweepy
consumer_key = "xxx"
consumer_secret = "xxx"
access_key = "xxx"
access_secret = "xxx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
print status.text
print status.user.screen_name
api.update_status("Insert Auto Reply Text Here", in_reply_to_status_id = status.id)
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(track=['@rishi'])
您需要为此创建一个开发者 Twitter 帐户并输入您自己的安全令牌。我目前无法测试写入时间线,因为我的是只读的。