我正在使用tweepy 4.10.1来使用StreamingClient获取Tweets,但是我无法加载任何media信息,甚至不能加载includes对象本身。我用get_tweet()方法尝试过类似的代码,media和includes都收到了很好的结果。
守则:
class TweetPrinter(tweepy.StreamingClient):
def on_tweet(self, tweet):
print(tweet.includes)
streaming_client = TweetPrinter('bearer-token')
streaming_client.add_rules(tweepy.StreamRule("from:xxxyyy"))
streaming_client.filter(tweet_fields=['author_id', 'created_at'],
media_fields=['preview_image_url', 'url'],
expansions=['attachments.media_keys'])
print(tweet.includes)我收到以下错误:
raise AttributeError from None当我在get_tweet()方法中使用相同的tweet id时,我可以从includes精细地检索media。
client = tweepy.Client(config.BEARER)
ID = 'xxxxyyyy'
tweet = client.get_tweet(ID,
tweet_fields=['author_id', 'created_at'],
media_fields=['preview_image_url', 'url'],
expansions=['attachments.media_keys'])
print(tweet.includes)根据谷歌,官方文档和常见问题解答,我已经尝试了我找到的所有推荐步骤。
media_fields和expansionsincludes应该是可用的,我在这里错过了什么?
发布于 2022-09-30 23:20:19
编辑
我发现使用on_data()是从tweet检索所有数据的正确方法。它涵盖了所有的tweet、includes和其他对象。
因此,正确的代码应该如下所示:
import orjson
class TweetPrinter(tweepy.StreamingClient):
def on_data(self, raw_data):
# Received as bytes, needs to be loaded by json (I use orjson)
raw_data = orjson.loads(raw_data)
print("data")
print(raw_data.get('data', None))
print("media")
print(raw_data.get('includes', None))不推荐溶液
当TweetPrinter类收到如下消息时,includes类应该包含要处理的函数:
class TweetPrinter(tweepy.StreamingClient):
def on_tweet(self, tweet):
print(tweet.data)
def on_includes(self, includes):
print(includes)多亏了这的文章,它帮助我找到了合适的解决方案:)
https://stackoverflow.com/questions/73914726
复制相似问题