用python写一个上传文件到蓝奏云的代码
时间: 2023-03-02 07:04:40 浏览: 325
好的,以下是Python上传文件到蓝奏云的示例代码:
```python
import requests
def upload_file_to_lanzouyun(file_path, folder_id, cookie):
# 设置请求头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Cookie": cookie
}
# 获取上传地址和token
url = "https://pc.woozooo.com/fileup.php"
params = {
"folder_id": folder_id
}
resp = requests.get(url, params=params, headers=headers)
if resp.status_code != 200:
raise Exception("获取上传地址和token失败")
data = resp.json()
if data["zt"] != 1:
raise Exception(data["info"])
# 上传文件
url = data["url"]
token = data["token"]
data = {
"task": "1",
"folder_id": folder_id,
"id": data["id"],
"upload_file": (file_path, open(file_path, "rb"), "application/octet-stream")
}
resp = requests.post(url, data=data, headers=headers, params={"task": "1", "token": token})
if resp.status_code != 200:
raise Exception("上传文件失败")
data = resp.json()
if data["zt"] != 1:
raise Exception(data["info"])
# 返回文件信息
return data["text"]
```
其中,`file_path`是要上传的文件的路径,`folder_id`是上传到的文件夹的ID,`cookie`是登录蓝奏云后的Cookie。
使用方法:
```python
file_path = "example.txt"
folder_id = "1234567890" # 文件夹ID需要自行获取
cookie = "这里填写你的Cookie"
result = upload_file_to_lanzouyun(file_path, folder_id, cookie)
print(result)
```
需要注意的是,这个示例代码仅供参考,实际使用时可能需要根据情况进行修改和调试。