gpt4 book ai didi

python - 流式传输 Twitter 推文时忽略转发

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:14 25 4
gpt4 key购买 nike

我正在尝试运行一个简单的脚本来流式传输实时推文。多次尝试过滤转发均未成功。我的信息流中仍然会收到手动转发(带有文本“RT @”)。我尝试过其他方法,包括 linklink

据我所知,我的代码与以下内容非常相似:link

我该怎么做才能忽略转发?

这是我的代码片段:

class StreamListener(tweepy.StreamListener):

def on_status(self, status):
if (status.retweeted) and ('RT @' not in status.text):
return

description = status.user.description
loc = status.user.location
text = status.text
coords = status.coordinates
geo = status.geo
name = status.user.screen_name
user_created = status.user.created_at
followers = status.user.followers_count
id_str = status.id_str
created = status.created_at
retweets = status.retweet_count
bg_color = status.user.profile_background_color

# Initialize TextBlob class on text of each tweet
# To get sentiment score from each class
blob = TextBlob(text)
sent = blob.sentiment

最佳答案

您可以做的是创建另一个函数来调用 StreamListener 中的 on_status 内部。这是对我有用的东西:

def analyze_status(text):
if 'RT' in text[0:3]:
print("This status was retweeted!")
print(text)
else:
print("This status was not retweeted!")
print(text)

class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
analyze_status(status.text)
def on_error(self, status_code):
print(status_code)

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth=twitter_api.auth, listener=myStreamListener)
myStream.filter(track=['Trump'])

这会产生以下结果:

This status was not retweeted!
@baseballcrank @seanmdav But they won't, cause Trump's name is on it. I can already hear their stupidity, "I hate D…
This status was retweeted!
RT @OvenThelllegals: I'm about to end the Trump administration with a single tweet
This status was retweeted!
RT @kylegriffin1: FLASHBACK: April 2016

SAVANNAH GUTHRIE: "Do you believe in raising taxes on the wealthy?"

TRUMP: "I do. I do. Inc…

这不是最优雅的解决方案,但我相信它解决了您面临的问题。

关于python - 流式传输 Twitter 推文时忽略转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43644286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com