📜  Python – Tweepy 中的 DirectMessage 对象

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

Python – Tweepy 中的 DirectMessage 对象

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

直接留言

Tweepy 模块中的DirectMessage对象包含有关地点的信息。

以下是 DirectMessage 对象中的属性列表:

示例:使用get_direct_message()方法获取直接消息。

# 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)
  
# ID of the direct message 
id = 1271013844639313927
    
# fetching the direct message 
direct_message = api.get_direct_message(id) 
  
# printing the information
print("The type is : " + direct_message.type)
print("The id is : " + direct_message.id)
print("The created_timestamp is : " + direct_message.created_timestamp)
  
# inside message_create
print("The recipient_id is : " + direct_message.message_create['target']['recipient_id'])
print("The sender_id is : " + direct_message.message_create['sender_id'])
print("The source_app_id is : " + direct_message.message_create['source_app_id'])
print("The text is : " + str(direct_message.message_create['message_data']['text']))
print("The entities are : " + str(direct_message.message_create['message_data']['entities']))
print("The media attachment is : " + str(direct_message.message_create['message_data']['attachment']))

输出 :