使用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 @ 2018-12-01 21:10  wangchuanli  阅读(2650)  评论(0编辑  收藏  举报