简体   繁体   中英

Access quoted_status object in Tweepy API.search_tweets

I have trouble in accessing parts of the objects that get returned from Tweepy's search_tweets API. I can easily navigate through most of the objects, but somehow I can't get access to the quoted_status sub-object.

This is an example of my code, just for one example Tweet:

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)

Example request for the query "#StandWithUkraine":

uk2 = api.search_tweets("StandWithUkraine", lang = "en", tweet_mode="extended", count =200)

Access the example Tweet that is a quote:

uk2[17].is_quote_status
TRUE

However, it is somehow not working to access the quoted status:

uk2[17].quoted_status
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-309-65b2969c2a40> in <module>
----> 1 uk2[17].quoted_status

AttributeError: 'Status' object has no attribute 'quoted_status'

Even though the object should include this:

Status(_api=<tweepy.api.API object at 0x0000013E4D367520>, _json={'created_at': 'Tue May 10 11:39:17 +0000 2022', 'id': 1523991057062670338, 'id_str': '1523991057062670338', 'full_text': "RT @OrangyCar: Sweet!!  Can't blame Russians who don't want to fight - who'd want to drive down a road like that?!\n\n#StandWithUkraine", 'truncated': False, 'display_text_range': [0, 133], 'entities': {'hashtags': [{'text': 'StandWithUkraine', 'indices': [116, 133]}], 'symbols': [], 'user_mentions': [{'screen_name': 'OrangyCar', 'name': 'Orange Lada', 'id': 1917214832, 'id_str': '1917214832', 'indices': [3, 13]}], 'urls': []}, 'metadata': {'iso_language_code': 'en', 'result_type': 'recent'}, 'source': '<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'user': {'id': 1171376597875793920, 'id_str': '1171376597875793920', 'name': 'sprizz324', 'screen_name': 'sprizz3241', 'location': 'Texas ', 'description': "Two degrees one a Masters. \nThree times retired,military,my trucking company,and last job working for someone else. \nanimal activist. NO MAGATS! NO DM's please!", 'url': None, 'entities': {'description': {'urls': []}}, 'protected': False, 'followers_count': 5688, 'friends_count': 4540, 'listed_count': 3, 'created_at': 'Tue Sep 10 10:55:54 +0000 2019', 'favourites_count': 60321, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 77440, 'lang': None, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': None, 'profile_background_image_url_https': None, 'profile_background_tile': False, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1171377534379986945/hfaB_BG5_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1171377534379986945/hfaB_BG5_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/1171376597875793920/1590842486', 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': True, 'default_profile': True, 'default_profile_image': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none', 'withheld_in_countries': []}, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'retweeted_status': {'created_at': 'Tue May 10 10:06:36 +0000 2022', 'id': 1523967733540573185, 'id_str': '1523967733540573185', 'full_text': "Sweet!!  Can't blame Russians who don't want to fight - who'd want to drive down a road like that?!\n\n#StandWithUkraine url", 'truncated': False, 'display_text_range': [0, 118], 'entities': {'hashtags': [{'text': 'StandWithUkraine', 'indices': [101, 118]}], 'symbols': [], 'user_mentions': [], 'urls': [{'url': 'url', 'expanded_url': 'https://twitter.com/GlasnostGone/status/1523953856807378946', 'display_url': 'twitter.com/GlasnostGone/s…', 'indices': [119, 142]}]}, 'metadata': {'iso_language_code': 'en', 'result_type': 'recent'}, 'source': '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>', 'in_reply_to_status_id': None, 'in_reply_to_status_id_str': None, 'in_reply_to_user_id': None, 'in_reply_to_user_id_str': None, 'in_reply_to_screen_name': None, 'user': {'id': 1917214832, 'id_str': '1917214832', 'name': 'Orange Lada', 'screen_name': 'OrangyCar', 'location': '', 'description': 'The WORLD #StandWithUkraine 🇺🇦🇺🇦🇺🇦', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': False, 'followers_count': 187, 'friends_count': 213, 'listed_count': 0, 'created_at': 'Sun Sep 29 14:04:23 +0000 2013', 'favourites_count': 43222, 'utc_offset': None, 'time_zone': None, 'geo_enabled': True, 'verified': False, 'statuses_count': 15802, 'lang': None, 'contributors_enabled': False, 'is_translator': False, 'is_translation_enabled': False, 'profile_background_color': 'C0DEED', 'profile_background_image_url': 'http://abs.twimg.com/images/themes/theme1/bg.png', 'profile_background_image_url_https': 'https://abs.twimg.com/images/themes/theme1/bg.png', 'profile_background_tile': False, 'profile_image_url': 'http://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png', 'profile_image_url_https': 'https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png', 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'has_extended_profile': False, 'default_profile': True, 'default_profile_image': True, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none', 'withheld_in_countries': []}, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, '**is_quote_status**': True, 'quoted_status_id': 1523953856807378946, '**quoted_status_id_str**': '1523953856807378946', **'quoted_status':** {'created_at': 'Tue May 10 09:11:27 +0000 2022', 'id': 1523953856807378946, 'id_str': '1523953856807378946', 'full_text': 'In east #Ukraine, a large Russian attack gets slaughtered by 🇺🇦 54th Mechanized Brigade. Same cannon fodder tactics of pouring tanks down a road. Advancing from occupied city of #Donetsk, the column was trying to reach nearby 🇺🇦 held town of Marinka. Full&gt; url1 url2', 'truncated': False, 'display_text_range': [0, 283], 'entities': {'hashtags': [{'text': 'Ukraine', 'indices': [8, 16]},

Also, it is not a problem to access the two previous parameters (also in bold) so I really don't get why it gives me an error:

uk2[17].quoted_status_id_str
'1523953856807378946'

uk2[17].is_quote_status
True

Maybe someone has encountered the same problem and can help me out. Also, I know that it is possible to access the quoted_status object when using the status API instead. However, I want to avoid using it because of the rate limit.

You can see that the fields you've marked in that JSON are within the retweeted_status field. You can access those fields, including quoted_status , through that field, eg uk2[17].retweeted_status.quoted_status .

The quoted_status you are trying to access is inside another nested object with key of retweeted_status . so you access it using

uk2[17].retweeted_status.quoted_status

just as @Harmon758 suggested.

The code

uk2[17].is_quote_status
TRUE

works fine because is_quote_status is in the primary uk2[17] json object and quoted_status is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM