简体   繁体   中英

How to only print the text of a tweet using Tweepy ? ( Python 3 )

I wrote this code in Python 3 using Tweepy, here's what I got:

Code:

import tweepy
auth = tweepy.OAuthHandler("secret :)", "secret :)")
auth.set_access_token("secret :)", "secret :)")

api = tweepy.API(auth)

status = api.search(q="Chuck Norris", count=1)
print (status)
text = status.text
print (text)

Output

C:\Python\venv\Scripts\python.exe C:/Python/main.py
[Status(_api=<tweepy.api.API object at 0x03718970>, _json={'created_at': 'Fri Feb 26 14:15:04 +0000 2021', 'id': 1365304377318211586, 'id_str': '1365304377318211586', 'text': '"Chuck Norris can kill a Shaolin Grandmaster with an Icecream."\n\n#KingBibi #BlaBla', 'truncated': False, 'entities': {'hashtags': [{'text': 'KingBibi', 'indices': [65, 74]}, {'text': 'BlaBla', 'indices': [75, 82]}], 'symbols': [], 'user_mentions': [], 'urls': []}, 'metadata': {'iso_language_code': 'en', 'result_type': 'recent'}, 'source': '<a href="http://example.com/" rel="nofollow">Test Test R2</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': 1203673488, 'id_str': '1203673488', 'name': 'Ariel Pinco', 'screen_name': 'ArielPinco', 'location': '', 'description': 'אולי יום יבוא ויהיה פה טוב.', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': False, 'followers_count': 15, 'friends_count': 95, 'listed_count': 0, 'created_at': 'Thu Feb 21 09:35:16 +0000 2013', 'favourites_count': 4580, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 26104, '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://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', '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': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none'}, 'geo': None, 'coordinates': None, 'place': None, 'contributors': None, 'is_quote_status': False, 'retweet_count': 0, 'favorite_count': 0, 'favorited': False, 'retweeted': False, 'lang': 'en'}, created_at=datetime.datetime(2021, 2, 26, 14, 15, 4), id=1365304377318211586, id_str='1365304377318211586', text='"Chuck Norris can kill a Shaolin Grandmaster with an Icecream."\n\n#KingBibi #BlaBla', truncated=False, entities={'hashtags': [{'text': 'KingBibi', 'indices': [65, 74]}, {'text': 'BlaBla', 'indices': [75, 82]}], 'symbols': [], 'user_mentions': [], 'urls': []}, metadata={'iso_language_code': 'en', 'result_type': 'recent'}, source='Test Test R2', source_url='http://example.com/', 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, author=User(_api=<tweepy.api.API object at 0x03718970>, _json={'id': 1203673488, 'id_str': '1203673488', 'name': 'Ariel Pinco', 'screen_name': 'ArielPinco', 'location': '', 'description': 'אולי יום יבוא ויהיה פה טוב.', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': False, 'followers_count': 15, 'friends_count': 95, 'listed_count': 0, 'created_at': 'Thu Feb 21 09:35:16 +0000 2013', 'favourites_count': 4580, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 26104, '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://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', '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': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none'}, id=1203673488, id_str='1203673488', name='Ariel Pinco', screen_name='ArielPinco', location='', description='אולי יום יבוא ויהיה פה טוב.', url=None, entities={'description': {'urls': []}}, protected=False, followers_count=15, friends_count=95, listed_count=0, created_at=datetime.datetime(2013, 2, 21, 9, 35, 16), favourites_count=4580, utc_offset=None, time_zone=None, geo_enabled=False, verified=False, statuses_count=26104, 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://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', profile_image_url_https='https://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', 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=False, following=False, follow_request_sent=False, notifications=False, translator_type='none'), user=User(_api=<tweepy.api.API object at 0x03718970>, _json={'id': 1203673488, 'id_str': '1203673488', 'name': 'Ariel Pinco', 'screen_name': 'ArielPinco', 'location': '', 'description': 'אולי יום יבוא ויהיה פה טוב.', 'url': None, 'entities': {'description': {'urls': []}}, 'protected': False, 'followers_count': 15, 'friends_count': 95, 'listed_count': 0, 'created_at': 'Thu Feb 21 09:35:16 +0000 2013', 'favourites_count': 4580, 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'verified': False, 'statuses_count': 26104, '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://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', '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': False, 'following': False, 'follow_request_sent': False, 'notifications': False, 'translator_type': 'none'}, id=1203673488, id_str='1203673488', name='Ariel Pinco', screen_name='ArielPinco', location='', description='אולי יום יבוא ויהיה פה טוב.', url=None, entities={'description': {'urls': []}}, protected=False, followers_count=15, friends_count=95, listed_count=0, created_at=datetime.datetime(2013, 2, 21, 9, 35, 16), favourites_count=4580, utc_offset=None, time_zone=None, geo_enabled=False, verified=False, statuses_count=26104, 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://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', profile_image_url_https='https://pbs.twimg.com/profile_images/1109543296316116997/3ZL6mr0j_normal.jpg', 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=False, following=False, follow_request_sent=False, notifications=False, translator_type='none'), geo=None, coordinates=None, place=None, contributors=None, is_quote_status=False, retweet_count=0, favorite_count=0, favorited=False, retweeted=False, lang='en')]
Traceback (most recent call last):
  File "C:/Python/main.py", line 11, in <module>
    text = status.text
AttributeError: 'SearchResults' object has no attribute 'text'

You can see that there's plenty of information there, but I only want to have the tweet's text.

Let's learn how to read documentation:

  1. Search the internet for Tweepy documentation -> https://docs.tweepy.org/en/latest/
  2. Look up the search method you want to use -> https://docs.tweepy.org/en/latest/api.html#search-methods
  3. It says 'Return type: SearchResults object. Find what that object is -> https://docs.tweepy.org/en/v3.10.0/search.html?q=SearchResults
  4. Turns out the documentation for this is missing -> consider using something other than Tweepy.
  5. You can now go check the Tweepy source or try to read the error... let's try the error
  6. 'SearchResults' object has no attribute 'text' means that the var you call status is actually of the type SearchResults . When you've printed it looked like a list, so let's check if it behaves like one:
results = api.search(q="Chuck Norris", count=1)
print(results[0])
# Output: Status(_api=<tweepy.api.API ...)
  1. So we can do status = results[0]
  2. Looking at the error, we see that the new status is now of type Status and from the error it is likely to have the fields mentioned in the representation ( _api , _json , id , created_at , text , entities , author ...)
  3. Wait! text ! Thats what we want!
api = tweepy.API(auth)
results = api.search(q="Chuck Norris", count=1)
status = results[0]
text = status.text
print(text)
# Output:
# "Chuck Norris can kill a Shaolin Grandmaster with an Icecream."
# 
# #KingBibi #BlaBla

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