📜  Python – Tweepy 中的 API.media_upload()

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

Python – Tweepy 中的 API.media_upload()

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

API.media_upload()

Tweepy 模块中 API 类的media_upload()方法用于将媒体对象上传到 Twitter。

示例 1:

Python3
# 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)
 
# the name of the media file
filename = "gfg.png"
 
# upload the file
media = api.media_upload(filename)
 
# printing the information
print("The media ID is : " + media.media_id_string)
print("The size of the file is : " + str(media.size) + " bytes")


Python3
# the name of the media file
filename = "gfg.png"
 
# upload the file
media = api.media_upload(filename)
 
# printing the dimensions
print("The width is : " + str(media.image['w']) + " pixels.")
print("The height is : " + str(media.image['h']) + " pixels.")


输出 :

The media ID is : 1270554007983910912
The size of the file is : 3346 bytes

示例 2:使用 media_upload() 方法查找图像文件的尺寸。

Python3

# the name of the media file
filename = "gfg.png"
 
# upload the file
media = api.media_upload(filename)
 
# printing the dimensions
print("The width is : " + str(media.image['w']) + " pixels.")
print("The height is : " + str(media.image['h']) + " pixels.")

输出 :

The width is : 225 pixels.
The height is : 225 pixels.