使用GitHub作Free图床

一、方法原理

  1、使用GitHub的Issues问题功能。

  新建一个Issues问题请求

  将图片拖拉或者上传到问题框后,会返回一个地址,这个地址就是图片直链

  

  2、通过访问github仓库内的图片地址并进行修改

例如我的image-host仓库,地址是:https://github.com/wangchuanli001/image-host/tree/master/imgtemp

对于对应的图片,例如snowtree.jpg

https://github.com/wangchuanli001/image-host/blob/master/imgtemp/snowtree.jpg(原地址)

进行更改后(可以作为直链访问):

https://github.com/wangchuanli001/image-host/blob/master/imgtemp/snowtree.jpg?raw=true

https://github.com/wangchuanli001/image-host/raw/master/imgtemp/snowtree.jpg

 

 二、拓展

  对于图片的上传只要记录下上传的文件路径以及文件名,就可以免费使用github作为图床使用了。

   1、通过爬虫进行爬取github上的内容,进行截取和拼接形成图片直链

    这个Python爬虫有编码问题,即gbk编码问题

复制代码
import re
import urllib.request
from bs4 import BeautifulSoup
url = "https://github.com/wangchuanli001/image-host/tree/master/imgtemp"

def getdoc():
    html_doc = urllib.request.urlopen(url).read()
    f = open("doc.txt","wb")
    f.write(html_doc)
    f.close()
def test():
    f = open("doc.txt","rb+")
    s = f.read()
    soup = BeautifulSoup(s,"html.parser",from_encoding="GB18030")
    print ("start")
    links = soup.select('a')
    for link in links:
        print (link)
    f.close()
复制代码

  2、通过图片上传工具进行记录

    记录本地文件路径和文件名,上传后生成直链。

复制代码
import os
# 从本地clone的仓库中得到文件列表
def fileListFunc(fileList,filePath,suffix):
    for filename in os.listdir(filePath):
        if os.path.isdir((filePath+"/"+filename)):
            # print (filePath+"/"+filename)
            fileListFunc(fileList,(filePath+"/"+filename),suffix)
        else:
            if filename.endswith(suffix):
                fileList.append(filePath+"/"+filename)
    return fileList
# 对列表中的文件进行字符串拼接成图片链接
def listHandler(fileList,filePath):
    for i in range(0,len(fileList)):
        fileList[i] = "<img src=\""+fileList[i].replace(filePath,"https://github.com/你的github用户名/image-host/raw/master")+"\" height=\"200\" width=\"200\">"
    fileList.append("-----------------------")
# 将图片链接追加到md文件的最后
def list2md(fileList):
    fo = open("image.md","a+")
    for item in fileList:
        fo.write(item)
    fo.close()

fileList = []
filePath = "E:\GitHub\Respositories\image-host"
fileLists = fileListFunc(fileList,filePath,"jpg")
listHandler(fileLists,filePath)
print (fileLists)
list2md(fileLists)
复制代码

 

posted @   wangchuanli  阅读(2691)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示