在这个 tweepy 脚本中在哪里排除转推? [英] Where to exclude retweets in this tweepy script?
本文介绍了在这个 tweepy 脚本中在哪里排除转推?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,它允许我通过 python 查看 1% 的 twitter firehose 流:
I have the following code which allows me to view a stream of 1% of the twitter firehose via python:
import sys
import tweepy
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
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):
if '' in status.text.lower():
print status.text
print status.coordinates
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=['example'])
我知道语法 include_rts = False
会从我正在查看的流中删除转发,但我不确定将其添加到上述代码的何处.
I know the syntax include_rts = False
will remove retweets from the stream I am viewing, but I am not sure where to add it to the above code.
有人可以帮忙吗?
谢谢
推荐答案
在监听器的 on_status 函数中添加以下条件:
Add the following condition to the on_status function in your listener:
def on_status(self, status):
if '' in status.text.lower() and 'retweeted_status' not in status:
print status.text
print status.coordinates
这篇关于在这个 tweepy 脚本中在哪里排除转推?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文