用python写登录https://account.aliyun.com/login/login.htm后跳转到一个页面下载文件
时间: 2023-05-26 07:02:21 浏览: 64
以下是Python代码实现:
```
import requests
import re
#登录阿里云
session = requests.session()
login_url = 'https://account.aliyun.com/login/login.htm'
response = session.get(login_url)
csrf_token = re.findall(r'name="_csrf_token" value="(.*?)"', response.text)[0]
login_data = {
"_csrf_token": csrf_token,
"resource": "",
"fromSite": 9,
"oauthToken": "",
"username": "your_username",
"password": "your_password"
}
session.post(login_url, data=login_data)
#下载文件
download_url = 'https://oss.console.aliyun.com/bucket/oss-cn-hangzhou/download/'+ 'your_file_url'
file = session.get(download_url)
with open('file_name', 'wb') as f:
f.write(file.content)
```
其中,需要替换`your_username`、`your_password`和`your_file_url`为实际的值。`file_name`为要保存的文件名。
首先,我们使用`requests`库创建一个`session`对象,并发送GET请求到登录页面`https://account.aliyun.com/login/login.htm`。从登录页面中提取出`_csrf_token`,用于后面的登录操作。然后,构造登录数据,包括`_csrf_token`、`username`和`password`等字段,使用`session.post`方法进行登录操作。
登录成功后,我们使用`session.get`方法请求要下载的文件的URL,得到文件内容,并将其保存到本地文件中。