代码拉取完成,页面将自动刷新
import re
import requests
from bs4 import BeautifulSoup
txtUserID = int(input('Username:'))
txtPasswd = input('Password:')
login_URL = 'http://222.30.63.15/NKEMIS/SystemLogin.aspx'
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.8",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
}
# 获取CSRF验证
response = requests.get(login_URL, headers=headers)
bsObj = BeautifulSoup(response.text, 'lxml')
__VIEWSTATE = bsObj.find('input').attrs['value']
__EVENTVALIDATION = bsObj.find('input', id='__EVENTVALIDATION').attrs['value']
from_data = {
"__VIEWSTATE": __VIEWSTATE,
"__EVENTVALIDATION": __EVENTVALIDATION,
"txtUserID": txtUserID,
"txtPasswd": txtPasswd,
"ImageButton1.x": 1,
"ImageButton1.y": 1,
}
session = requests.session()
post = session.post(login_URL, data=from_data)
index_url = 'http://222.30.63.15/NKEMIS/Student/StudentDefault.aspx'
index_html = session.get(index_url) # 成功抓取
# print(index_html)
tbody_bsObj = BeautifulSoup(index_html.text, 'lxml')
# 课程相关标题
tr = tbody_bsObj.find('tr', {'class':'dgrdHeader'}).find_all('td')
for title, students_number, homework in zip(tr[0], tr[1], tr[-1]):
print(title + '\t\t', students_number + '\t\t', homework + '\t\t')
all_title = tbody_bsObj.find_all('a', style="color:red")
for title in all_title:
print(title.get_text().strip())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。