我想上传一个以上的图片每推特推特。我能够使它为一张图片工作,但当我试图上传更多的照片,然后出现问题。我试图实现我在互联网上看到的其他解决方案,但没有成功。也许也是因为我刚刚开始学习Python,不能正确理解错误。
这就是我所写的:
# auth on tw
auth = tweepy.OAuthHandler(API_KEY, APP_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCCESS_TOKEN_SECRET)
# create API obj
api = tweepy.API(auth)
# create tweet
tweet = "Test with imgs"
pics = ["ninja.jpg", "ninja2.jpg"]
media_ids = [api.media_upload(i).media_id_string for i in pics]
status = api.update_status_with_media(filename = media_ids, status = tweet)
我得到的错误如下:
Traceback (most recent call last):
File "/home/bog/2test.py", line 31, in <module>
status = api.update_status_with_media(filename = media_ids, status = tweet)
File "/home/bog/.local/lib/python3.9/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
File "/home/bog/.local/lib/python3.9/site-packages/tweepy/api.py", line 1181, in update_status_with_media
files = {'media[]': stack.enter_context(open(filename, 'rb'))}
TypeError: expected str, bytes or os.PathLike object, not list
编辑:我还尝试了以下代码:
# create tweet
tweet = "Test con img"
pics = ["ninja.jpg", "ninja2.jpg"]
media_ids = []
for pic in pics:
res = api.media_upload(pic)
media_ids.append(res.media_id)
status = api.update_status_with_media(media_ids = media_ids, status = tweet)
print(media_ids)
我得到了一个错误:
Traceback (most recent call last):
File "/home/bog/2test.py", line 34, in <module>
status = api.update_status_with_media(media_ids = media_ids, status = tweet)
File "/home/bog/.local/lib/python3.9/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
TypeError: update_status_with_media() missing 1 required positional argument: 'filename'
发布于 2021-12-31 14:54:25
不要使用update_status_with_media()
。它是不推荐的,并且无论如何都不接受media_ids
作为参数。使用update_status()
并传入media_id_string
列表。
这应该有效:status = api.update_status(media_ids = media_ids, status = tweet)
https://docs.tweepy.org/en/stable/api.html#tweepy.API.update_status
https://stackoverflow.com/questions/70436566
复制相似问题