我想把硬盘上的一张照片上传到电讯报。在文档中,它说要使用upload_file():
telegraph.upload.upload_file(f)将文件上传到Telegra.ph的服务器。返回链接列表。只允许.jpg、.jpeg、.png、.gif和.mp4文件。参数:f (file,str或list) -文件名或类文件对象.
但我不明白"f (file,str或list) -文件名或类文件对象“是什么意思。也就是说,我需要对照片做什么,这样它才能传递给这个函数。
我试图解决这个问题:
upload_file(open('1.png', 'rb'))错误: telegraph.exceptions.TelegraphException:文件类型无效
myf = io.StringIO()
myf.write(open(f'photo/{i}.png', 'rb'))
print(upload_file(myf))
myf.close()错误: TypeError:字符串参数被期望,得到'_io.BufferedReader‘
发布于 2020-12-03 18:06:14
我使用这种方法加载图像
def upload_image_telegraph(path_image):
with open(path_image, 'rb') as f:
result_requests = requests.post(
'http://telegra.ph/upload',
files={'file': ('file', f, 'image/jpg')} # image/gif, image/jpeg,image/jpg, image/png, video/mp4
).json()
return result_requests您还可以使用库html_telegraph_poster及其帮助进行下载,您可以同时传输本地图像和指向它们的链接。
from html_telegraph_poster import upload_image
res = upload_image('local path image or link image') https://stackoverflow.com/questions/64177086
复制相似问题