如何使用Tweepy只打印tweet的文本?(Python 3)



我使用Tweepy在Python 3中编写了这段代码,下面是我得到的结果:

<标题>代码:
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)
<标题>输出
C:PythonvenvScriptspython.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."nn#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."nn#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'

你可以看到那里有很多信息,但我只想要tweet的文本。

让我们学习如何阅读文档:

  1. 在互联网上搜索Tweepy文档->https://docs.tweepy.org/en/latest/
  2. 查找要使用的搜索方法->https://docs.tweepy.org/en/latest/api.html搜索方法
  3. 它说'返回类型:SearchResults对象。找出那个对象是什么->https://docs.tweepy.org/en/v3.10.0/search.html?q=SearchResults
  4. 原来这方面的文档缺失了->考虑使用Tweepy以外的东西
  5. 你现在可以去查看微博源或尝试读取错误…让我们试试错误
  6. 'SearchResults' object has no attribute 'text'意味着您称为status的变量实际上是SearchResults类型。当你打印出来的时候,它看起来像一个列表,所以让我们检查一下它是否像一个列表:
results = api.search(q="Chuck Norris", count=1)
print(results[0])
# Output: Status(_api=<tweepy.api.API ...)
  1. 所以我们可以输入status = results[0]
  2. 查看错误,我们看到新的status现在是Status类型,并且从错误中很可能具有表示中提到的字段(_api,_json,id,created_at,text,entities,author…)
  3. 等等!text!这就是我们想要的!
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

最新更新