Skip to content

Instantly share code, notes, and snippets.

@ecnelises
Last active August 14, 2018 18:09
Show Gist options
  • Save ecnelises/7c714c48f2b49acffc6611b5a42feeaa to your computer and use it in GitHub Desktop.
Save ecnelises/7c714c48f2b49acffc6611b5a42feeaa to your computer and use it in GitHub Desktop.
从同济大学选课网站上爬取信息,输出绩点(支持统一认证登录)
require 'mechanize'
# xuanke.tongji.edu.cn 现采用统一认证登录,原登录方式不再使用
class NewGpaSpider
# 学期内所有课程
attr_reader :courses
def initialize(userid, password, term)
@agent = Mechanize.new
welcome_page = @agent.get('http://xuanke.tongji.edu.cn')
rp1 = @agent.click(welcome_page.search('font')[2].children[0])
rp2 = @agent.get(rp1.parser.at('meta[http-equiv="refresh"]')['content'][/url=(.+)/, 1])
rp3 = rp2.forms[0].submit
login_form = rp3.form('IDPLogin')
login_form['Ecom_User_ID'] = userid
login_form['Ecom_Password'] = password
rp4 = @agent.submit(login_form)
rp5 = @agent.get('https://ids.tongji.edu.cn:8443/nidp/saml2/sso?sid=0&sid=0')
rp6 = rp5.forms[0].submit
rp7 = @agent.get('http://xuanke.tongji.edu.cn/tj_login/redirect.jsp'+
'?link=/tj_xuankexjgl/score/query/student/cjcx.jsp' +
'?qxid=20051013779916&mkid=20051013779901&qxid=20051013779916')
rp7.forms[0].fields[1].value = term
@main_page = rp7.forms[0].submit
get_courses
end
# 总绩点
def overall_gpa
@overall_gpa ||= statistic_params(1)
end
# 总学分
def overall_credits
@overall_credits ||= statistic_params(6)
end
# 挂科学分
def failed_credits
@failed_credits ||= statistic_params(8)
end
#挂科数量
def failed_courses
@failed_courses ||= statistic_params(13)
end
# 学期平均绩点
def term_gpa
@term_gpa ||= @main_page.search('font').last.inner_text.strip
end
# 学期名
def term_name
@term_name ||= @main_page.css('#T1').children[7].children[1].children[0].children[0].text
end
private
def statistic_params(id)
@main_page.css('#T1').children[3].children[0].children[id].children[0].text.strip
end
def get_courses
start_rank = 11
@courses = []
start_dom = @main_page.css('#T1').children
while start_dom[start_rank]&.children&.[](5)
@courses << (1..17).step(2).map do |nid|
start_dom[start_rank].children[nid].children[0].children[0].children[0].text
end
start_rank += 2
end
end
end
@sid = '你的学号'
@pwd = '你的密码'
@term = '你的学期' # 比如 20172 就是 2017-2018 第 2 学期
spider = NewGpaSpider.new(@sid, @pwd, @term)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment