簡體   English   中英

如何使用 Tweepy 僅打印推文的文本? ( Python 3 )

[英]How to only print the text of a tweet using Tweepy ? ( 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)

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'

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

讓我們學習如何閱讀文檔:

  1. 在互聯網上搜索Tweepy 文檔 -> https://docs.tweepy.org/en/latest/
  2. 查找您要使用的搜索方法 -> https://docs.tweepy.org/en/latest/api.html#search-methods
  3. 它說'返回類型: SearchResults object。 找到 object 是什么 -> https://docs.tweepy.org/en/v3.10.0/search.html?q=SearchResults
  4. 結果發現缺少此文檔->考慮使用 Tweepy 以外的東西。
  5. 您現在可以 go 檢查 Tweepy 源或嘗試讀取錯誤...讓我們嘗試錯誤
  6. 'SearchResults' object has no attribute 'text'意味着您調用的 var 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_jsonidcreated_attextentitiesauthor ...)
  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

暫無
暫無

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

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