1. 程式人生 > 其它 >python實現部落格園自動上傳圖片

python實現部落格園自動上傳圖片

python實現部落格園自動上傳圖片

需要發表部落格的時候,有些部落格圖片很多,要是一張一張手動上傳,就太慢了,而且還很卡頓。
所以想著能不能自動上傳來著。
網上找了一些方案,比如設定圖床啥的,但是利用其他平臺,總感覺不太方便。

本來試了試碼雲的,但是現在好像不能直接訪問了。

最終還是決定,自己動手,豐衣足食。

果然還是python做這種事情比較簡單。

1.使用fiddler抓取上傳圖片的資料包

fiddler分析網頁資料果然是很順手啊。

可以看見,上傳圖片之類的,就會有這種分隔符。這是開頭部分,緊接著就是圖片的原始資料了。

這是結尾部分,圖片資料結束後,還要這麼一些東西。

方便起見,直接寫死成bytearray了。

2.python程式碼
import requests
import re

#請首先設定自己的Cookie:開啟部落格園,登入以後,自己控制檯抓包再複製到這裡就好了
cookie = ''
newline = bytearray(b'\r\n')
'''
------WebKitFormBoundary0B5IAB61P3SBVPIR
Content-Disposition: form-data; name="imageFile"; filename="QQ图片20220528140941.jpg"
Content-Type: image/jpeg


'''
begin = bytearray(b'\x2D\x2D\x2D\x2D\x2D\x2D\x57\x65\x62\x4B\x69\x74\x46\x6F\x72\x6D\x42\x6F\x75\x6E\x64\x61\x72\x79\x30\x42\x35\x49\x41\x42\x36\x31\x50\x33\x53\x42\x56\x50\x49\x52\x0D\x0A\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x44\x69\x73\x70\x6F\x73\x69\x74\x69\x6F\x6E\x3A\x20\x66\x6F\x72\x6D\x2D\x64\x61\x74\x61\x3B\x20\x6E\x61\x6D\x65\x3D\x22\x69\x6D\x61\x67\x65\x46\x69\x6C\x65\x22\x3B\x20\x66\x69\x6C\x65\x6E\x61\x6D\x65\x3D\x22\x51\x51\xE5\x9B\xBE\xE7\x89\x87\x32\x30\x32\x32\x30\x35\x32\x38\x31\x34\x30\x39\x34\x31\x2E\x6A\x70\x67\x22\x0D\x0A\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x54\x79\x70\x65\x3A\x20\x69\x6D\x61\x67\x65\x2F\x6A\x70\x65\x67\x0D\x0A\x0D\x0A')
'''
Content-Disposition: form-data; name="host"

www.cnblogs.com
------WebKitFormBoundary0B5IAB61P3SBVPIR
Content-Disposition: form-data; name="uploadType"

Paste
------WebKitFormBoundary0B5IAB61P3SBVPIR--
'''
end = bytearray(b'\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x44\x69\x73\x70\x6F\x73\x69\x74\x69\x6F\x6E\x3A\x20\x66\x6F\x72\x6D\x2D\x64\x61\x74\x61\x3B\x20\x6E\x61\x6D\x65\x3D\x22\x68\x6F\x73\x74\x22\x0D\x0A\x0D\x0A\x77\x77\x77\x2E\x63\x6E\x62\x6C\x6F\x67\x73\x2E\x63\x6F\x6D\x0D\x0A\x2D\x2D\x2D\x2D\x2D\x2D\x57\x65\x62\x4B\x69\x74\x46\x6F\x72\x6D\x42\x6F\x75\x6E\x64\x61\x72\x79\x30\x42\x35\x49\x41\x42\x36\x31\x50\x33\x53\x42\x56\x50\x49\x52\x0D\x0A\x43\x6F\x6E\x74\x65\x6E\x74\x2D\x44\x69\x73\x70\x6F\x73\x69\x74\x69\x6F\x6E\x3A\x20\x66\x6F\x72\x6D\x2D\x64\x61\x74\x61\x3B\x20\x6E\x61\x6D\x65\x3D\x22\x75\x70\x6C\x6F\x61\x64\x54\x79\x70\x65\x22\x0D\x0A\x0D\x0A\x50\x61\x73\x74\x65\x0D\x0A\x2D\x2D\x2D\x2D\x2D\x2D\x57\x65\x62\x4B\x69\x74\x46\x6F\x72\x6D\x42\x6F\x75\x6E\x64\x61\x72\x79\x30\x42\x35\x49\x41\x42\x36\x31\x50\x33\x53\x42\x56\x50\x49\x52\x2D\x2D\x0D\x0A')
pattern1 = re.compile("!\[image-.*].*") #檢測是不是圖片
pattern2 = re.compile("\w:.+.[png|jpeg|jpg|pjpg|svg]")  #提取本地圖片連結

#讀取檔案不應該失敗的,返回圖片原始資料
def getData(path):
        file = open(path,"rb")
        data = file.read()
        return data

#把本地圖片上傳到部落格園並返回生成的連結
def postPicture(path):
    url = 'https://upload.cnblogs.com/imageuploader/CorsUpload'
    header = {
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Microsoft Edge";v="101"',
        'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary0B5IAB61P3SBVPIR',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46',
        'Referer': 'https://i.cnblogs.com/',
        'Origin':'https://i.cnblogs.com',
        'Host':'upload.cnblogs.com',
        'Sec-Fetch-Site':'same-site',
        'Sec-Fetch-Mode':'cors',
        'Sec-Fetch-Dest':'empty',
        'Accept':'application/json, text/plain, */*',
        'X-XSRF-TOKEN':'CfDJ8AuMt_3FvyxIgNOR82PHE4nIDV5Pg5w60oZXbLwvLvGKC-rtwdKwC8Pgkcwe5OOloIi4jWTSsaMH4-QLvoBYQ37nXdzoLiASGZqMb0wLY7sWukYYtT2leLmPMULAlwEl2iUR3aLx0IakUsto58Zpmu3so1VLzcIGOU61gkoXAQXetR77pvBoMHWsp6EEukoIbg',
        'Cookie':cookie
        }
    data = bytearray(getData(path))
    data += newline
    total = begin + data + end  #構造post資料,只有圖片內容需要改動,其餘都是固定的
    web = requests.post(url,data=total, headers=header)
    js = web.json()
    if js['success']==True:
        return js['message']
    else:
        print(js['message'])
    return path

#解析md檔案,把本地圖片連結替換為生成連結,並生成替換連結後的md檔案
def parseMd(path):
    file = open(path,"r",encoding="utf-8")
    newfile = open(path + "_add.md","w",encoding="utf-8")
    for line in file.readlines():
        str = re.findall(pattern1,line)
        if len(str)>0:
            str = str[0]
            print(str)
            str = re.findall(pattern2, str)
            if len(str)>0:
                str = str[0]
                print(str)
                newstr = postPicture(str)
                print(newstr)
                line = line.replace(str,newstr)
        newfile.write(line)
    file.close()
    newfile.close()

parseMd("1.md")
def testMatch():
    path = r"![image-20220528113706359](https://img2022.cnblogs.com/other/1852902/202205/1852902-20220528203514047-1170780136.jpg)"
    pattern = re.compile("!\[image-.*].*")
    str = re.findall(pattern, path)[0]
    print(str)
    pattern = re.compile("\w:.+.[png|jpeg|jpg|pjpg|svg]")
    str = re.findall(pattern, str)[0]
    print(str)
    print(path.replace(str, "hello"))

思路也就是自己構造圖片上傳的post資料;

成功以後,再做好MD解析的工作,也就是提取出圖片的本地地址;

最後綜合起來,就完成了。

可惜重要的cookie不能簡單的通過js獲取,寫個登入也挺麻煩,不如直接網頁抓包取呢。