📜  Python – Tweepy 中的 API.destroy_direct_message()

📅  最后修改于: 2022-05-13 01:54:52.230000             🧑  作者: Mango

Python – Tweepy 中的 API.destroy_direct_message()

Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。

API.destroy_direct_message()

Tweepy 模块中API类的destroy_direct_message()方法用于删除直接消息。

示例 1:

# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# direct message ID
id = print("Before using the destroy_direct_message() object")
if api.get_direct_message(id):
    print("This direct message exists.")
  
# deleting the direct message
api.destroy_direct_message(id)
  
  
print("\nAfter using the destroy_direct_message() object")
try:
    api.get_direct_message(id)
except:
    print("This direct message no longer exists.")

输出 :

Before using the destroy_direct_message() object
This direct message exists.

After using the destroy_direct_message() object
This direct message no longer exists.

示例 2:尝试删除不存在的直接消息将引发异常。

# invalid direct message ID
id = 12345
  
# deleting the direct message
api.destroy_direct_message(id)

输出 :

raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]