为什么状态“不可下标”,我该如何解决? Python Tweepy

问题描述

当有人用我的@handle回复该推文时,我试图使用下面编写的代码获取该推文中视频的媒体URL:

import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
import json

class StdOutListener(StreamListener):
    def on_data(self,data):
        clean_data = json.loads(data)
        tweetId = clean_data['id']
        tweet_name = clean_data['user']['screen_name']
auth = tweepy.OAuthHandler("consumer_key","consumer_secret")
            auth.set_access_token("access_token","access_token_secret")
            api = tweepy.API(auth)
            mediaupload_id = clean_data['in_reply_to_status_id']
            origtweet_media = api.get_status(mediaupload_id)
            print()
            print(origtweet_media)
            native_media = origtweet_media['extended_entities']['media'][0]['video_info']
            tweet_media = native_media['variants'][0]['url']
            print(tweet_media)

def setUpAuth():
    auth = tweepy.OAuthHandler("consumer_key","consumer_secret")
    auth.set_access_token("access_token","access_token_secret")
    api = tweepy.API(auth)
    return api,auth

def followStream():
    api,auth = setUpAuth()
    listener = StdOutListener()
    stream = Stream(auth,listener)
    stream.filter(track=["@YOUR_TWITTER_HANDLE"],is_async=True)

if __name__ == "__main__":
    followStream()

一切似乎都设置正确,但是当我运行代码时,有人回复带有视频的推文(回复者提到我的@handle),我得到此错误

Exception has occurred: TypeError
'Status' object is not subscriptable
    native_media = origtweet_media['extended_entities']['media'][0]['video_info']

为什么“状态”不可下标?并且有什么方法可以解决此问题,以便它成功获取视频网址(tweet_media)?

请帮助我!

解决方法

已解决!

仔细查看print版本的origtweet_media之后,我注意到该推文信息位于_json=()内部。因此,我在._json的末尾添加了origtweet_media = api.get_status(mediaupload_id),因为这只是隔离了json部分,使我能够找到tweet_media的视频URL。这是新的更新行的样子:

origtweet_media = api.get_status(mediaupload_id)._json

然后成功运行!