简体   繁体   English

访问 Tweepy API.search_tweets 中的quoted_status 对象

[英]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.我无法访问从 Tweepy 的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.我可以轻松浏览大多数对象,但不知何故我无法访问quoted_status子对象。

This is an example of my code, just for one example Tweet:这是我的代码示例,仅作为 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":查询“#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.另外,我知道在使用 status API 时可以访问quoted_status对象。 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.您可以看到您在该 JSON 中标记的字段位于retweeted_status字段中。 You can access those fields, including quoted_status , through that field, eg uk2[17].retweeted_status.quoted_status .您可以通过该字段访问这些字段,包括quoted_status ,例如uk2[17].retweeted_status.quoted_status

The quoted_status you are trying to access is inside another nested object with key of retweeted_status .您尝试访问的quoted_status位于另一个嵌套对象中,其键为retweeted_status so you access it using所以你使用它访问它

uk2[17].retweeted_status.quoted_status

just as @Harmon758 suggested.正如@Harmon758建议的那样。

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.工作正常,因为is_quote_status在主要的 uk2[17] json 对象中,而quoted_status不在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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