簡體   English   中英

訪問 Tweepy API.search_tweets 中的quoted_status 對象

[英]Access quoted_status object in Tweepy API.search_tweets

我無法訪問從 Tweepy 的search_tweets API 返回的部分對象。 我可以輕松瀏覽大多數對象,但不知何故我無法訪問quoted_status子對象。

這是我的代碼示例,僅作為 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)

查詢“#StandWithUkraine”的示例請求:

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

訪問引用的示例推文:

uk2[17].is_quote_status
TRUE

但是,以某種方式無法訪問引用的狀態:

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'

即使對象應包括以下內容:

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]},

此外,訪問前面的兩個參數(也是粗體)也不是問題,所以我真的不明白為什么它會給我一個錯誤:

uk2[17].quoted_status_id_str
'1523953856807378946'

uk2[17].is_quote_status
True

也許有人遇到了同樣的問題,可以幫助我。 另外,我知道在使用 status API 時可以訪問quoted_status對象。 但是,由於速率限制,我想避免使用它。

您可以看到您在該 JSON 中標記的字段位於retweeted_status字段中。 您可以通過該字段訪問這些字段,包括quoted_status ,例如uk2[17].retweeted_status.quoted_status

您嘗試訪問的quoted_status位於另一個嵌套對象中,其鍵為retweeted_status 所以你使用它訪問它

uk2[17].retweeted_status.quoted_status

正如@Harmon758建議的那樣。

編碼

uk2[17].is_quote_status
TRUE

工作正常,因為is_quote_status在主要的 uk2[17] json 對象中,而quoted_status不在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM