微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Tweepy 仅打印推文的文本? ( 蟒蛇 3 )

如何解决如何使用 Tweepy 仅打印推文的文本? ( 蟒蛇 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:\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',Metadata={'iso_language_code': 'en',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=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,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>,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. Search the internet 用于 Tweepy 文档 -> https://docs.tweepy.org/en/latest/
  2. 查找您要使用的搜索方法 -> https://docs.tweepy.org/en/latest/api.html#search-methods
  3. 它说'返回类型:SearchResults 对象。找出那个对象是什么 -> https://docs.tweepy.org/en/v3.10.0/search.html?q=SearchResults
  4. 发现缺少相关文档 -> 考虑使用 Tweepy 以外的其他工具。
  5. 您现在可以去检查 Tweepy 源或尝试阅读错误...让我们尝试错误
  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,{ {1}}、_jsonidcreated_attextentities ...)
  3. 等等! author!这就是我们想要的!
text

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。