目标网站:https://www.txrjy.com/forum.php
一、进入网页,右键“检查”
二、输入用户名和密码,点击“登录”,点击“Network”,上划加载项找到蓝色框中的内容
三、点击第一个加载项,找到URL
四、相关代码:
import requests
from bs4 import BeautifulSoup
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36',
}
data = {
'username': 'your_username',
'password': 'your_password',
}
url = 'https://www.txrjy.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes'
session = requests.Session()
session.post(url, headers=headers, data=data)
# 登录后,我们需要获取另一个网页中的内容
response = session.get('https://www.txrjy.com/forum.php', headers=headers)
# 解析网页内容,提取用户名和用户组
soup = BeautifulSoup(response.text, 'html.parser')
userinfo = soup.find('div', class_='userlogin')
username = userinfo.find('strong').text
usergroup = userinfo.find('a', href=lambda x: x and x.startswith('home.php?mod=spacecp&ac=usergroup')).text
print("用户名:", username)
print("用户组:", usergroup)