gpt4 book ai didi

python - 如何使用 tweepy 流式传输 Twitter 提及?

转载 作者:太空狗 更新时间:2023-10-29 17:27:24 26 4
gpt4 key购买 nike

我目前正在使用以下代码,它获取推文,然后将其传递给处理的函数。但这不是实时的。

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
mentions = api.mentions_timeline(count=1)
for mention in mentions:
processText()

我也研究过流式推文,但我还没有找到流式传输提及的方法。

最佳答案

您可以使用 Streaming API过滤包含@mentions 的推文。当您使用 track 参数过滤流时,通过在您的 track 参数中包含您感兴趣的用户名来过滤,如下所示:

stream.filter(track=['twitterapi'])

这将实时向您返回所有包含字符串 "twitterapi" 的推文,包括 @mentions(即 "@twitterapi")。查看Twitter Streaming API track parameter这是如何工作的。然后,您可以使用结果 JSON 仅获取实际 @mention 您感兴趣的用户的推文。结果 JSON 如下所示:

{"retweet_count":0,"text":"Man I like me some @twitterapi","entities":{"urls":[],"hashtags":[],"user_mentions":[{"indices":[19,30],"name":"Twitter API","id":6253282,"screen_name":"twitterapi","id_str":"6253282"}]},"retweeted":false,"in_reply_to_status_id_str":null,"place":null,"in_reply_to_user_id_str":null,"coordinates":null,"source":"web","in_reply_to_screen_name":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,"favorited":false,"contributors":null,"geo":null,"truncated":false,"created_at":"Wed Feb 29 19:42:02 +0000 2012","user":{"is_translator":false,"follow_request_sent":null,"statuses_count":142,"profile_background_color":"C0DEED","default_profile":false,"lang":"en","notifications":null,"profile_background_tile":true,"location":"","profile_sidebar_fill_color":"ffffff","followers_count":8,"profile_image_url":"http:\/\/a1.twimg.com\/profile_images\/1540298033\/phatkicks_normal.jpg","contributors_enabled":false,"profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/365782739\/doof.jpg","description":"I am just a testing account, following me probably won't gain you very much","following":null,"profile_sidebar_border_color":"C0DEED","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1540298033\/phatkicks_normal.jpg","default_profile_image":false,"show_all_inline_media":false,"verified":false,"profile_use_background_image":true,"favourites_count":1,"friends_count":5,"profile_text_color":"333333","protected":false,"profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/365782739\/doof.jpg","time_zone":"Pacific Time (US & Canada)","created_at":"Fri Sep 09 16:13:20 +0000 2011","name":"fakekurrik","geo_enabled":true,"profile_link_color":"0084B4","url":"http:\/\/blog.roomanna.com","id":370773112,"id_str":"370773112","listed_count":0,"utc_offset":-28800,"screen_name":"fakekurrik"},"id":174942523154894848,"id_str":"174942523154894848"}

那是一团糟,但是如果您查看 "entities" 对象,您会看到 "user_mentions":

"user_mentions":[{"indices":[19,30],"name":"Twitter API","id":6253282,"screen_name":"twitterapi","id_str":"6253282"}]

然后,在 "screen_name" 下,您会看到 "twitterapi",向您显示用户 @twitterapi被@提及。

关于python - 如何使用 tweepy 流式传输 Twitter 提及?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115442/

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