1. 爬取 https://authserver.hnca.edu.cn/authserver/login 页面“账号登录” tab页下form表单里面的用户名和密码输入框的name属性的值,并将结果保存到“data.txt”文件中。
时间: 2023-12-30 10:06:06 浏览: 25
以下是使用 Python 爬取 https://authserver.hnca.edu.cn/authserver/login 页面“账号登录” tab页下form表单里面的用户名和密码输入框的name属性的值,并将结果保存到“data.txt”文件中的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送 GET 请求,获取登录页面
url = 'https://authserver.hnca.edu.cn/authserver/login'
response = requests.get(url)
# 解析 HTML 页面
soup = BeautifulSoup(response.text, 'html.parser')
# 获取用户名输入框和密码输入框的 name 属性值
username_input = soup.find('input', {'name': 'username'})['name']
password_input = soup.find('input', {'name': 'password'})['name']
# 将结果保存到文件中
with open('data.txt', 'w', encoding='utf-8') as f:
f.write(f'Username input name: {username_input}\n')
f.write(f'Password input name: {password_input}\n')
```
这段代码首先发送 GET 请求,获取登录页面的 HTML 内容,然后使用 BeautifulSoup 解析 HTML 页面。通过查看页面源代码,我们可以发现用户名输入框和密码输入框都是在 form 表单中的,因此我们可以使用 `soup.find()` 方法找到这两个输入框元素,并获取它们的 name 属性值。最后,我们将结果保存到“data.txt”文件中。