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 = https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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=https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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 = https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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=https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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=https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/{"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 = https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/{"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 = https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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=https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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 = https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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=https://www.cnblogs.com/xxsyxxAI/archive/2022/08/06/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.uj5u.com/houduan/501168.html
標籤:其他
