使用 Tweepy 和 Python 在 Twitter 上上传媒体
Upload media on Twitter using Tweepy with Python
我正在尝试从 Url 和 post 上的推特上截取图片
但是我得到了这个让我很着急的错误。
TypeError: expected str, bytes or os.PathLike object, not JpegImageFile
from PIL import Image
import urllib.request
import tweepy
def get_picture(picture,file_path):
full_path = file_path + '.jpg'
urllib.request.urlretrieve(picture,full_path)
def twitter_api():
consumer_key = 'XXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
picture = (f"https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3")
path=r'C:\....\...\....\.......\images\.jpg'
text=("Youpi It works!")
get_picture(picture,"images/")
imageok=picture=Image.open(path)
twitter_api().update_status_with_media(text,imageok)
如果有人得到线索,将不胜感激!
我认为问题出在这一行:imageok=picture=Image.open(path)
这只是一个错字吗?
忘记 Pillow
。它需要磁盘上图像的路径(如 str
、bytes
或 os.PathLike object
)
twitter_api().update_status_with_media(text, path_to_image)
但是你输入的 Pillow.Inage
是 JpegImageFile
并且你在错误消息中有它
expected str, bytes or os.PathLike object, not JpegImageFile
完整的工作代码
import urllib.request
import tweepy
import os
def get_picture(url, full_path):
urllib.request.urlretrieve(url, full_path)
def twitter_api():
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
# --- main ---
url = f"https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
#path = r'C:\....\...\....\.......\images.jpg'
path = 'images.jpg'
text = "Youpi It works!"
get_picture(url, path)
twitter_api().update_status_with_media(text, path)
如果你想发送文件而不保存在磁盘上或用 Pillow
生成,那么它有选项 file=
可以打开文件或对象 io.BytesIO
- 然后你可以将互联网上的数据直接放入 io.BytesIO
并在不保存到磁盘的情况下使用它
import urllib.request
import tweepy
import os
import io
def twitter_api():
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
# --- main ---
url = "https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
text = "Testing module tweepy"
data = urllib.request.urlopen(url).read()
file_like_object = io.BytesIO(data)
twitter_api().update_status_with_media(text, 'fake_name.jpg', file=file_like_object)
使用 Pillow 将图像转换为灰度并发送到 Twitter 的示例 - 所有这些都没有在磁盘上保存文件。
from PIL import Image
import urllib.request
import tweepy
import os
import io
def twitter_api():
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
# --- main ---
url = "https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
text = "Testing module tweepy"
data = urllib.request.urlopen(url).read()
file_like_object = io.BytesIO(data)
image = Image.open(file_like_object)
#image.show()
grayscale = image.convert('L')
#grayscale.show()
file_like_object = io.BytesIO()
grayscale.save(file_like_object, 'jpeg')
file_like_object.seek(0) # move to the beginning of file
twitter_api().update_status_with_media(text, 'fake_name.jpg', file=file_like_object)
我正在尝试从 Url 和 post 上的推特上截取图片 但是我得到了这个让我很着急的错误。
TypeError: expected str, bytes or os.PathLike object, not JpegImageFile
from PIL import Image
import urllib.request
import tweepy
def get_picture(picture,file_path):
full_path = file_path + '.jpg'
urllib.request.urlretrieve(picture,full_path)
def twitter_api():
consumer_key = 'XXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
picture = (f"https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3")
path=r'C:\....\...\....\.......\images\.jpg'
text=("Youpi It works!")
get_picture(picture,"images/")
imageok=picture=Image.open(path)
twitter_api().update_status_with_media(text,imageok)
如果有人得到线索,将不胜感激!
我认为问题出在这一行:imageok=picture=Image.open(path)
这只是一个错字吗?
忘记 Pillow
。它需要磁盘上图像的路径(如 str
、bytes
或 os.PathLike object
)
twitter_api().update_status_with_media(text, path_to_image)
但是你输入的 Pillow.Inage
是 JpegImageFile
并且你在错误消息中有它
expected str, bytes or os.PathLike object, not JpegImageFile
完整的工作代码
import urllib.request
import tweepy
import os
def get_picture(url, full_path):
urllib.request.urlretrieve(url, full_path)
def twitter_api():
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
# --- main ---
url = f"https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
#path = r'C:\....\...\....\.......\images.jpg'
path = 'images.jpg'
text = "Youpi It works!"
get_picture(url, path)
twitter_api().update_status_with_media(text, path)
如果你想发送文件而不保存在磁盘上或用 Pillow
生成,那么它有选项 file=
可以打开文件或对象 io.BytesIO
- 然后你可以将互联网上的数据直接放入 io.BytesIO
并在不保存到磁盘的情况下使用它
import urllib.request
import tweepy
import os
import io
def twitter_api():
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
# --- main ---
url = "https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
text = "Testing module tweepy"
data = urllib.request.urlopen(url).read()
file_like_object = io.BytesIO(data)
twitter_api().update_status_with_media(text, 'fake_name.jpg', file=file_like_object)
使用 Pillow 将图像转换为灰度并发送到 Twitter 的示例 - 所有这些都没有在磁盘上保存文件。
from PIL import Image
import urllib.request
import tweepy
import os
import io
def twitter_api():
consumer_key = os.getenv('TWITTER_CONSUMER_KEY')
consumer_secret = os.getenv('TWITTER_CONSUMER_SECRET')
access_token = os.getenv('TWITTER_ACCESS_TOKEN')
access_token_secret = os.getenv('TWITTER_ACCESS_TOKEN_SECRET')
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
# --- main ---
url = "https://www.iheartradio.ca/image/policy:1.15731844:1627581512/rick.jpg?f=default&$p$f=20c1bb3"
text = "Testing module tweepy"
data = urllib.request.urlopen(url).read()
file_like_object = io.BytesIO(data)
image = Image.open(file_like_object)
#image.show()
grayscale = image.convert('L')
#grayscale.show()
file_like_object = io.BytesIO()
grayscale.save(file_like_object, 'jpeg')
file_like_object.seek(0) # move to the beginning of file
twitter_api().update_status_with_media(text, 'fake_name.jpg', file=file_like_object)