python爬虫

# -*- coding: utf-8 -*-

#Author:xiepan

import http.cookiejar
import urllib.request
import re
import urllib
from bs4 import BeautifulSoup

class Spider(object):
    def __init__(self):
        print('获取课程成绩和学分,GO!')

    def get_login(self,login_url,post_data,headers):
        cookie = http.cookiejar.LWPCookieJar()  ###声明一个cookiejar来保存cookie信息
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))  ###创建cookie处理器,并通过handler来构建opener
        post_data = urllib.parse.urlencode(post_data).encode(encoding='GBK')
        request = urllib.request.Request(login_url, post_data, headers)
        response = opener.open(request)
        response = opener.open(grade_url)
        return response.read().decode('GBK')

if __name__=='__main__':
    info={}
    ##登录URL
    login_url = 'http://grdms.bit.edu.cn/yjs/login.do'
    ##成绩URL
    grade_url = 'http://grdms.bit.edu.cn/yjs/yanyuan/py/pychengji.do?method=enterChaxun'
    post_data = {'j_username': '2120140383',
                 'j_password': '********',
                 'loginType': '0'
                 }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
        'Referer': 'http://grdms.bit.edu.cn/yjs/login.jsp',
    }
    my_spider=Spider()
    my_html=my_spider.get_login(login_url,post_data,headers)
    soup = BeautifulSoup(my_html, 'html.parser')
    every_class = soup.find_all('tr', bgcolor="#FFFFFF", height="23")
    for each_class in every_class:
        each_class = str(each_class)
        try:
            myItems = re.findall('<td align="left">(.*?)</td>', each_class, re.S)
            info['title'] = myItems[3]
            info['grade'] = myItems[9]
            print(info)
        except IndexError:
            print('第一栏标题')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值