【pyhon】批量图片上传图床

批量图片上传图床

需求

  1. 图片都储存在一个文件夹内,地址为“media”
  2. 将文件夹内图片批量上传到图床网站
  3. 将上传后地址以markdown形式写入文本文件

代码

import os
def get_all_files(folder_path):
    files = []
    for filename in os.listdir(folder_path):
        file_path = os.path.join(folder_path, filename)
        if os.path.isfile(file_path):
            files.append(filename)
    return files


def numerical_sort(value):
    # 提取字符串中的数字部分并将其转换为整数,进行排序
    numbers = ''.join(filter(str.isdigit, value))
    return int(numbers)


folder_path = "media"
file_names = get_all_files(folder_path)
file_names = sorted(file_names, key=lambda x: numerical_sort(x.zfill(10)))
for i in range(len(file_names)):
    file_names[i] = folder_path + "/" + file_names[i]
print(file_names)


import requests
import os


def get(filename):
    # 获取上传图片的地址
    url = "https://www.imgurl.org/api/v2/upload"
    files = {'file': open(filename, 'rb')}
    data = {'file': files,
            'uid': '6bb1c33da03e2cd8df6c69f30301456b',
            'token': '1be42595824ab2a882b4c6264236aeba'}
    headers = {
        'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203'

    }
    response = requests.post(url, files=files, data=data, headers=headers)
    json = response.json()
    print(json)
    return "%s" % (json['data']['url'])


filelist = file_names
f = open('图片链接地址.txt', 'a')
for pictures in filelist:
    f.write("![](%s)\n" % get(pictures))
f.close()
print('over!')

结果

控制台
D:\User\anaconda\python.exe "D:\文档\Coder\pythonTest\上传图片 图床\main.py" 
['media/image1.png', 'media/image2.png', 'media/image3.png', 'media/image4.png', 'media/image5.png', 'media/image6.png', 'media/image7.png', 'media/image8.png', 'media/image9.png', 'media/image10.png', 'media/image11.png', 'media/image12.png', 'media/image13.png', 'media/image14.png', 'media/image15.png', 'media/image16.png', 'media/image17.png', 'media/image18.png', 'media/image19.png', 'media/image20.png', 'media/image21.png', 'media/image22.png', 'media/image23.png', 'media/image24.png', 'media/image25.png', 'media/image26.png', 'media/image27.png', 'media/image28.png', 'media/image29.png', 'media/image30.png', 'media/image31.png', 'media/image32.png', 'media/image33.png']
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/471936fb2530b3ae.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/471936fb2530b3ae.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/471936fb2530b3ae_thumb.png', 'image_width': 5020, 'image_height': 3220, 'client_name': 'image1.png', 'id': 1667245, 'imgid': '471936fb2530b3ae', 'delete': 'https://www.imgurl.org/delete/e1a5f80a5dc222c5'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/920fe430ba300616.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/920fe430ba300616.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/920fe430ba300616_thumb.png', 'image_width': 5010, 'image_height': 6310, 'client_name': 'image2.png', 'id': 1667246, 'imgid': '920fe430ba300616', 'delete': 'https://www.imgurl.org/delete/fe334866ed987c1b'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/37aa21d64bd50d75.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/37aa21d64bd50d75.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/37aa21d64bd50d75_thumb.png', 'image_width': 4220, 'image_height': 4020, 'client_name': 'image3.png', 'id': 1667247, 'imgid': '37aa21d64bd50d75', 'delete': 'https://www.imgurl.org/delete/b00f43b195bef4ac'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/7589f5498ad7979c.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/7589f5498ad7979c.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/7589f5498ad7979c_thumb.png', 'image_width': 729, 'image_height': 397, 'client_name': 'image4.png', 'id': 1667248, 'imgid': '7589f5498ad7979c', 'delete': 'https://www.imgurl.org/delete/f521d89469bdc334'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/7622e9abf9272890.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/7622e9abf9272890.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/7622e9abf9272890_thumb.png', 'image_width': 733, 'image_height': 408, 'client_name': 'image5.png', 'id': 1667249, 'imgid': '7622e9abf9272890', 'delete': 'https://www.imgurl.org/delete/2348bbbf4457e80a'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/925b2a4904fa4140.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/925b2a4904fa4140.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/925b2a4904fa4140_thumb.png', 'image_width': 1024, 'image_height': 1024, 'client_name': 'image6.png', 'id': 1667250, 'imgid': '925b2a4904fa4140', 'delete': 'https://www.imgurl.org/delete/eb6a8048f8d7abbb'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/6149cf20b07619a6.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/6149cf20b07619a6.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/6149cf20b07619a6_thumb.png', 'image_width': 606, 'image_height': 304, 'client_name': 'image7.png', 'id': 1667251, 'imgid': '6149cf20b07619a6', 'delete': 'https://www.imgurl.org/delete/8b57da1de4f09280'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/b9323565b46bdabd.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/b9323565b46bdabd.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/b9323565b46bdabd_thumb.png', 'image_width': 606, 'image_height': 304, 'client_name': 'image8.png', 'id': 1667252, 'imgid': 'b9323565b46bdabd', 'delete': 'https://www.imgurl.org/delete/d9d93044a7c49b28'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/fb536623d43ef93b.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/fb536623d43ef93b.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/fb536623d43ef93b_thumb.png', 'image_width': 606, 'image_height': 304, 'client_name': 'image9.png', 'id': 1667253, 'imgid': 'fb536623d43ef93b', 'delete': 'https://www.imgurl.org/delete/041c659316833648'}}
{'code': 200, 'msg': '', 'data': {'relative_path': 'imgs/2023/08/26/2810dac92ddbcf49.png', 'url': 'https://s3.bmp.ovh/imgs/2023/08/26/2810dac92ddbcf49.png', 'thumbnail_url': 'https://s3.bmp.ovh/imgs/2023/08/26/2810dac92ddbcf49_thumb.png', 'image_width': 606, 'image_height': 304, 'client_name': 'image10.png', 'id': 1667254, 'imgid': '2810dac92ddbcf49', 'delete': 'https://www.imgurl.org/delete/b920fee45900eba7'}}
{'code': -1000, 'msg': '今日上传数量上限!', 'data': ''}
markdown结果
![](https://s3.bmp.ovh/imgs/2023/08/26/471936fb2530b3ae.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/920fe430ba300616.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/37aa21d64bd50d75.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/7589f5498ad7979c.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/7622e9abf9272890.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/925b2a4904fa4140.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/6149cf20b07619a6.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/b9323565b46bdabd.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/fb536623d43ef93b.png)
![](https://s3.bmp.ovh/imgs/2023/08/26/2810dac92ddbcf49.png)
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值