美文网首页
python爬虫之爬取教务网成绩

python爬虫之爬取教务网成绩

作者: iCoder_ | 来源:发表于2017-01-13 20:24 被阅读0次

    python爬虫之爬取教务网成绩

    这次的内容主要就是讲述自己的第一只python爬虫,而所要爬取的对象就是学校的教务网。促使我写出了这样一只python爬虫的主要的原因就是学校查成绩太过于麻烦。成绩不是一次性的全部公布,而是一科一科的不定时的公布,所以我就决定自己编写一只爬虫,让它自己运行,然后将爬取得到的成绩自动的发送到邮箱里。这样我就不用自己再去教务网查成绩。好了,废话不多说,开始吧!

    第一步:
    在开始真正的编写爬虫之前我们要首先明白我们平时手动的查询成绩是怎样实现的,大概的过程是怎样,提交的数据有哪些,数据是提交到哪儿的。为了弄明白这些我们可以利用一个插件,即HttpFox,可以利用火狐浏览器安装这个插件,其他的便不再赘述。
    第二步:
    得到上面要求的数据后便开始编写爬虫代码。主要如下:

    #-*- coding:utf-8 -*-
    
    import urllib
    import urllib2
    import cookielib
    from bs4 import BeautifulSoup
    from email.mime.text import MIMEText 
    import smtplib 
    from email.header import Header
    
    #Login and Query
    def login(user_name,pass_word,efdf):
            #get cookie
            cookie = cookielib.CookieJar()
            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
            #login data
            postdata = urllib.urlencode({
                "_VIEWSTATEGEN":"CAA0A5A7",
                "Sel_Type":"STU",
                "txt_dsdsdsdjkjkjc":user_name,
                "txt_dsdfdfgfouyy":pass_word,
                "txt_ysdsdsdskgf":"",
                "pcInfo":"", 
                "typeName":"",
                "aerererdsdxcxdfgfg":"",
                "efdfdfuuyyuuckjg":efdf
                })
    
            # Login
            loginUrl = "http://202.202.1.176:8080/_data/index_login.aspx"
            html1 = opener.open(loginUrl,postdata)
    
            # Query score
            gradeUrl2 = "http://202.202.1.176:8080/xscj/Stu_MyScore_rpt.aspx"
            postdata2 = urllib.urlencode({
                "sel_xn":"2016",
                "sel_xq":"0",
                "SJ":"0",
                "SelXNXQ":"2",
                "zfx_flag":"0",
                "zxf":"0"
                })
            html2 = opener.open(gradeUrl2,postdata2)
    
            #deal data
            soup = BeautifulSoup(html2.read(),"html.parser")
            content = soup.select('td')
    
            # output result
            str_content='Scores\n'
            i=0
            while i<len(content):
                if(i==0 or i==1):
                    print content[i].text
                    str_content=str_content+content[i].text+'\n'
                    i=i+1
                else:
                    for j in range(10):
                        if (i+j) < len(content):
                            print (content[i+j].text),
                            str_content = str_content+content[i+j].text+'     '
                            if j == 9:
                                print ("\n")
                                str_content = str_content+'\n'
                        else:
                            break
                    i = i+10
            return str_content
    
    #send email
    def Send_email(receiver,send_content):
            mail_host="smtp.qq.com"
            mail_user="1432864950@qq.com"
            mail_pass="************"     #这儿是邮箱的授权码
    
            message=MIMEText(send_content,'plain','utf-8')
            message['From']=Header(mail_user,'utf-8')
            message['To']=Header(receiver,'utf-8')
    
            subject = 'Score'
            message['Subject']=Header(subject,'utf-8')
            try:
                smtpObj = smtplib.SMTP_SSL(mail_host,465)
                smtpObj.login(mail_user,mail_pass)
                smtpObj.sendmail(mail_user,receivers,message.as_string())
                smtpObj.quit()
                return ['Send email success']
            except smtplib.SMTPException,e:
                return ['There must have some error']
            return 
    
    result1 = login("20154302","*****","70982C84EDDBBDBF2F28546AF2A6FA")
    Send_email("3344963462@qq.com",result1)
    

    这篇文章并不是一篇python爬虫的教程文章,只是用于记录我自己的一些经历,所以有很多的细节的地方都没有讲解。

    相关文章

      网友评论

          本文标题:python爬虫之爬取教务网成绩

          本文链接:https://www.haomeiwen.com/subject/rqwsbttx.html