Python – Tweepy 中的媒体对象
Twitter是一个流行的社交网络,用户在其中分享称为推文的消息。 Twitter 允许我们使用 Twitter API 或Tweepy挖掘任何用户的数据。数据将是从用户那里提取的推文。首先要做的是从 twitter 开发人员那里轻松获得每个用户可用的消费者密钥、消费者密钥、访问密钥和访问密钥。这些密钥将帮助 API 进行身份验证。
媒体
Tweepy 模块中的Media
对象包含有关在 Twitter 上上传的媒体文件的信息。
以下是 Media 对象中的属性列表:
- media_id : The ID of the media object.
- media_id_str : The ID of the media object as a string.
- size : The size of the media object in bytes.
- expires_after_secs : The time in seconds after which the media object expires.
- image_type : The type of image.
- w : The width of the media object.
- h : The height of the media object.
示例:我们将使用media_upload()
方法上传和获取媒体对象。
考虑下图:
# 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)
# uploading the media and fetching the Media object
media = api.media_upload("gfg.png")
# printing the information
print("The media_id is : " + str(media.media_id))
print("The media_id_string is : " + media.media_id_string)
print("The size is : " + str(media.size))
print("The expires_after_secs is : " + str(media.expires_after_secs))
print("The image_type is : " + str(media.image["image_type"]))
print("The w is : " + str(media.image["w"]))
print("The h is : " + str(media.image["h"]))
输出 :
The media_id is : 1273526215773573121
The media_id_string is : 1273526215773573121
The size is : 3346
The expires_after_secs is : 86400
The image_type is : image/png
The w is : 225
The h is : 225
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。