百木园-与人分享,
就是让自己快乐。

7个比较稳定的国内图床API 图片上传接口 (Python实战)

1.七个网站的Python实战代码

1.汽车之家

def t1():
    #汽车之家
    ##请上传JPG、JPEG、BMP格式图片
    import requests
    from requests_toolbelt import MultipartEncoder
    from bs4 import BeautifulSoup
    cookie = \'\'\'

    \'\'\'.replace(\"\\r\", \"\").replace(\"\\n\", \"\").replace(\" \", \"\").encode(\'utf-8\')
    header = {
        \"Cookie\": cookie,
        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"
    }

    data = MultipartEncoder(fields={
        \"uploadFile\": (
            \'0.jpg\', open(r\'C:\\Users\\26670\\Pictures\\0.jpg\', \'rb\'), \"image/jpeg\")})
    header[\'Content-Type\'] = data.content_type
     				         print(BeautifulSoup(requests.post(\'https://i.autohome.com.cn/setting/face\', headers=header, data=data).text,\'lxml\').select(\'#preview1\'))

2.起点中文网

def t2():
    #起点中文网
    import requests
    from requests_toolbelt import MultipartEncoder
    from bs4 import BeautifulSoup
    cookie = \'\'\'

    \'\'\'.replace(\"\\r\", \"\").replace(\"\\n\", \"\").replace(\" \", \"\").encode(\'utf-8\')
    header = {
        \"Cookie\":cookie,
        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"
    }
    data = MultipartEncoder(fields={
        \"_csrfToken\": \"y4z3yEFehOVEEMyDgrTQCuBKrxFhI7c41W8te9vp\",
        \"image\": (
            \'1.png\', open(r\'D:\\PycharmProjects\\pythonProject\\selenium_chrome\\edu\\1.png\', \'rb\'), \"image/png\")})
    header[\'Content-Type\'] = data.content_type
    return \"https://\" + BeautifulSoup(
        requests.post(\"https://my.qidian.com/ajax/headimage/uploadimg\", headers=header, data=data).text,\'lxml\').body.string.replace(\"\\\\\", \"\").split(\"//\")[1][:-3] + \".png\"

3.喜马拉雅

def t3():
    # 喜马拉雅
    import time
    import requests
    from requests_toolbelt import MultipartEncoder
    from bs4 import BeautifulSoup
    import os
    import hashlib
    import datetime
    from urllib import parse
    # step 1
    cookie = \'\'\'

    \'\'\'.replace(\"\\r\", \"\").replace(\"\\n\", \"\").replace(\" \", \"\").encode(\'utf-8\')
    header = {
        \"Cookie\": cookie,
        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"
    }
    name = \'0.jpg\'
    path = r\"C:\\Users\\26670\\Pictures\\0.jpg\"
    rsp1 = requests.post(\'https://cupload.ximalaya.com/clamper-token/token\', data={\"fileName\": name,
                                                                                   \"fileSize\": str(
                                                                                       os.path.getsize(path)),
                                                                                   \"uploadType\": \"picture\",
                                                                                   \"callerType\": \"ting\"},
                         headers=header).json()[\'token\']
    print(rsp1)
    # step 2
    # Content-Type application/octet-stream 这里的文件上传方式我还不清楚,因为MD5那关没过去
    header = {\"Authorization\": str(rsp1).encode(\'utf-8\'),
              \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"}
    time2 = str(datetime.datetime.now().strftime(\"%Y/%m/%d %H:%M:%S\")).replace(\'/0\', \"/\")
    MD5 = str(\'\')  # 请原谅我太菜了,看不懂 JS代码,不明白这里离得 MD5加密方式, 可能是 token结合某个量吧,希望会的大神告诉咱怎么弄清楚这个
    url={
        \"id\":\"WU_FILE_0\",
        \"name\":name,
        \"type\":\"image/png\",
        \"lastModifiedDate\":time2,
        \"size\":str(os.path.getsize(path)),
        \"timeStart\":str(int(time.time() * 1000)),
        \"chunks\":\"1\",
        \"chunk\":\"0\",
        \"multipartId\":\"\",
        \"md5\":MD5,
        \"uid\":\"0\",}
    ctx = str(requests.post(url=\'https://cupload.ximalaya.com/upload/file/blk?\'+parse.urlencode(url) , headers=header, files={\'files\': open(path, \'rb\')}).json()[\'data\'][\'ctx\']).encode(\'utf-8\')
    multipartId = str(requests.post(url=\'https://cupload.ximalaya.com/upload/file/blk?\'+parse.urlencode(url), headers=header, files={\'files\': open(path, \'rb\')}).json()[\'data\'][\'multipartId\']).encode(\'utf-8\')
    # step3
    # 获取link
    header = {\"Authorization\": str(rsp1).encode(\'utf-8\'),
              \"Cookie\": cookie,
              \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"}
    data = {\"uid\": 0,
            \"uploadType\": \"picture\",
            \"appkey\": \"ting\",
            \"fileSize\": str(os.path.getsize(path)),
            \"fileExtName\": \"jpg\",  # 如果是png图片那就是 png
            \"fileName\": name,
            \"deviceType\": \"web\",
            \"clientIp\": \"\",
            \"ctxList\": [ctx],
            \"multipartId\": multipartId,
            \"codeOrNot\": \"1\"}
    print(requests.post(\'https://cupload.ximalaya.com/upload/merge/mkfile\', headers=header, json=data)[\'data\'][
              \'freeFileUrl\'])

4.中关村在线

def t4():
    # 中关村在线
    # 仅支持jpg、png格式,且文件小于2M
    import requests
    from requests_toolbelt import MultipartEncoder
    from bs4 import BeautifulSoup
    cookie = \'\'\'
    
    \'\'\'.replace(\"\\r\", \"\").replace(\"\\n\", \"\").replace(\" \", \"\").encode(\'utf-8\')
    header = {
        \"Cookie\": cookie,
        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"
    }

    data = MultipartEncoder(fields={
        \"myPhoto\": (
            \'0.jpg\', open(r\'C:\\Users\\26670\\Pictures\\0.jpg\', \'rb\'), \"image/jpeg\")})
    header[\'Content-Type\'] = data.content_type
    print(requests.post(\'https://my.zol.com.cn/index.php?c=Ajax_User&a=uploadImg\', headers=header, data=data).json()[
              \'url\'])

5.失效:蘑菇街

6.失效:VIVO手机论坛

7.花瓣网

def t7():
    # 花瓣网
    import requests
    from requests_toolbelt import MultipartEncoder
    cookie = \'\'\'
    
       \'\'\'.replace(\"\\r\", \"\").replace(\"\\n\", \"\").replace(\" \", \"\").encode(\'utf-8\')
    header = {
        \"Cookie\": cookie,
        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0\"
    }
    data = MultipartEncoder(fields={
        \"file\": (
            \'0.jpg\', open(r\'C:\\Users\\26670\\Pictures\\0.jpg\', \'rb\'), \"image/jpeg\")})
    header[\'Content-Type\'] = data.content_type
    print(requests.post(\'https://api.huaban.com/upload\', headers=header, data=data).json()[
              \'key\'])  # https://gd-hbimg.huaban.com/

2.总结

【content-type :multipart/form-data; boundary=】类型,没有验证手段的简单易行,有验证手段的需要参考JS判断加密方式,如上述“喜马拉雅”的实战,我没做出来,因为加密方式没弄懂,JavaScript代码经过加密之后,完全看不懂,也没用解密的手段。

3.参考文献

[1]: https://blog.csdn.net/xiaojianpitt/article/details/6856536	\"Multipart/form-data POST文件上传详解\"
[2]: https://zhuanlan.zhihu.com/p/106860812	\"7个比较稳定的国内图床API 图片上传接口\"
[3]: https://mp.weixin.qq.com/s?__biz=MzI5MTg1NjA4Nw==&mid=2247548954&idx=1&sn=cb11f2eb5bcfe6349d695acc3755c1aa&chksm=ec080ba2db7f82b4195276e3ff6892ca346d7abbde0dab4c2eb3fe46582b74d5113665592a18&scene=21#wechat_redirect	\"Python实现各种加密,接口加解密不再难\"
[4]: https://blog.csdn.net/weixin_41646716/article/details/85070981	\"options请求\"

来源:https://www.cnblogs.com/xxsyxxAI/p/16557886.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » 7个比较稳定的国内图床API 图片上传接口 (Python实战)

相关推荐

  • 暂无文章