【问题标题】:How do I Upload more images per tweet with Tweepy如何使用 Tweepy 为每条推文上传更多图像
【发布时间】:2022-01-22 22:34:54
【问题描述】:

我想在 Twitter 上为每条推文上传不止一张图片。 我能够使它适用于一张图片,但是当我尝试上传更多图片时,问题就出现了。我尝试实施我在互联网上看到的其他解决方案,但没有成功。可能也是因为我刚开始学习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'

【问题讨论】:

    标签: python tweepy twitterapi-python


    【解决方案1】:

    不要使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      • 2015-11-08
      • 2019-03-06
      相关资源
      最近更新 更多