0
所以我寫了一個bot,從維基百科(帶請求)中提取圖像並將它們發佈到twitter(與twython)。我發現this,這使我相信我可以做類似twython upload_media from tempfiles
import tempfile
import twython
import requests
...
req = requests.get(img_url, stream=True)
with tempfile.TemporaryFile() as img_file:
for chunk in req:
img_file.write(req)
resp = twython_client.upload_media(media=img_file)
return resp['media_id']
但upload_media
調用拋出400S。像
...
with open('tmp_img_file', 'wb') as img_file:
for chunk in req:
img_file.write(chunk)
with open('tmp_img_file', 'rb') as img_file:
resp = twython_client.upload_media(media=img_file)
os.remove('tmp_img_file')
return resp['media_id']
東西確實工作,但不是「創造被使用後立即刪除臨時文件」 的臨時文件的整點?我錯過了什麼/做錯了什麼?