芝加哥艺术学院的tweepy,上传媒体错误API =无效的参数
尝试使用Twitter机器人玩乐。
这个想法是:根据芝加哥API艺术学院的说法,发布了一条推文,其中包含信息(艺术家,日期,地点...)和媒体(图片)。
我无法在这里上传媒体,波纹管您可以看到我要修复的追溯。
我会很感激! b
import tweepy
import requests
import random
import time
import io
############################# My logs ######################################
def twitter_api():
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
############################# My fonctions #################################
############################# The Loop ######################################
while True:
get_number()
r = requests.get(f"https://api.artic.edu/api/v1/artworks/{get_number()}")
a = r.json()
get_Titre(),get_Artist(),get_Date(),get_Place(),get_Im()
requests2 = (f"https://www.artic.edu/iiif/2/{get_Im()}/full/843,/0/default.jpg")
print("Imge ok:....................", requests2)
print(type(requests2))
message = (get_Titre()+ get_Artist()+str(get_Date())+get_Place())
print("La tête du tweet sera:", message)
twitter_api().update_status_with_media(message,requests2)
time.sleep(14400)
这是追溯:
Traceback (most recent call last):
File "C:\PycharmProjects\TwitterBot\main.py", line 76, in <module>
twitter_api().update_status_with_media(message,requests2)
File "C:\PycharmProjects\TwitterBot\venv\lib\site-packages\tweepy\api.py", line 46, in wrapper
return method(*args, **kwargs)
File "C:\PycharmProjects\TwitterBot\venv\lib\site-packages\tweepy\api.py", line 1181, in update_status_with_media
files = {'media[]': stack.enter_context(open(filename, 'rb'))}
OSError: [Errno 22] Invalid argument: 'https://www.artic.edu/iiif/2/904ea189-c852-5f84-c614-a26a851f9b74/full/843,/0/default.jpg'
Trying to have fun with a twitter bot.
The idea is : according to the art institute of chicago API, posting a Tweet with the informations (Artist, Date, Place...) And the media (picture).
I can't upload a media here, bellow you can see the traceback that I am trying to fix.
I will appreciate !
B
import tweepy
import requests
import random
import time
import io
############################# My logs ######################################
def twitter_api():
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api
############################# My fonctions #################################
############################# The Loop ######################################
while True:
get_number()
r = requests.get(f"https://api.artic.edu/api/v1/artworks/{get_number()}")
a = r.json()
get_Titre(),get_Artist(),get_Date(),get_Place(),get_Im()
requests2 = (f"https://www.artic.edu/iiif/2/{get_Im()}/full/843,/0/default.jpg")
print("Imge ok:....................", requests2)
print(type(requests2))
message = (get_Titre()+ get_Artist()+str(get_Date())+get_Place())
print("La tête du tweet sera:", message)
twitter_api().update_status_with_media(message,requests2)
time.sleep(14400)
Here is the Traceback :
Traceback (most recent call last):
File "C:\PycharmProjects\TwitterBot\main.py", line 76, in <module>
twitter_api().update_status_with_media(message,requests2)
File "C:\PycharmProjects\TwitterBot\venv\lib\site-packages\tweepy\api.py", line 46, in wrapper
return method(*args, **kwargs)
File "C:\PycharmProjects\TwitterBot\venv\lib\site-packages\tweepy\api.py", line 1181, in update_status_with_media
files = {'media[]': stack.enter_context(open(filename, 'rb'))}
OSError: [Errno 22] Invalid argument: 'https://www.artic.edu/iiif/2/904ea189-c852-5f84-c614-a26a851f9b74/full/843,/0/default.jpg'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅文件名。
但是第三个参数可以是
类似文件的对象
,这意味着具有函数.read()
的对象。如果您要使用
urllib.request
,则它给出具有.Read()
的对象,并且可以使用。顺便说一句:您必须将任何文本用作第二个参数 - 可以是假文件名,但功能需要它。
使用
请求
您必须使用io.bytesio
才能创建类似文件的对象
编辑:
最终您可以使用<代码> stream = true ,然后
response.raw
给出类似文件的对象
,但这并不那么流行。See documentation for update_status_with_media - second argument has to be filename.
But third argument can be
file-like object
and this means object which has function.read()
.If you would use
urllib.request
then it gives object which has.read()
and it works.BTW: you have to use any text as second argument - can be fake filename but function needs it.
With
requests
you have to useio.BytesIO
to createfile-like object
EDIT:
Eventually you can use
stream=True
and thenresponse.raw
givesfile-like object
but this is not so popular.