首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Tweepy在每条推特上上传更多图片

如何用Tweepy在每条推特上上传更多图片
EN

Stack Overflow用户
提问于 2021-12-21 13:52:40
回答 1查看 210关注 0票数 0

我想上传一个以上的图片每推特推特。我能够使它为一张图片工作,但当我试图上传更多的照片,然后出现问题。我试图实现我在互联网上看到的其他解决方案,但没有成功。也许也是因为我刚刚开始学习Python,不能正确理解错误。

这就是我所写的:

代码语言:javascript
运行
复制
# 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)

我得到的错误如下:

代码语言:javascript
运行
复制
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

编辑:我还尝试了以下代码:

代码语言:javascript
运行
复制
# 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)

我得到了一个错误:

代码语言:javascript
运行
复制
    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'
EN

回答 1

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70436566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档