python 爬虫 处理超级课程表传输的数据

借鉴的别人的思路

http://www.oschina.net/code/snippet_2463131_53711

抓取超级课程表传输的数据

他的传输数据居然是明文的……

现在已经把自己的课表都抓出来了

不过这个也是抓取手机APP数据的思路?

还是值得学习一番的。

//很蠢……

//我干嘛不直接爬学校的教务处呢

#!/usr/local/bin/python2.7
# -*- coding: utf8 -*-
"""
  超级课程表话题抓取
"""
import urllib2
from cookielib import CookieJar
import json


''' 读Json数据 '''
def fetch_data(json_data):
    data = json_data['data']
    timestampLong = data['timestampLong']
    messageBO = data['messageBOs']
    topicList = []
    for each in messageBO:
        topicDict = {}
        if each.get('content', False):
            topicDict['content'] = each['content']
            topicDict['schoolName'] = each['schoolName']
            topicDict['messageId'] = each['messageId']
            topicDict['gender'] = each['studentBO']['gender']
            topicDict['time'] = each['issueTime']
            print each['schoolName'],each['content']
            topicList.append(topicDict)
    return timestampLong, topicList


''' 加载更多 '''
def load(timestamp, headers, url):
    headers['Content-Length'] = '159'
    loadData = 'timestamp=%s&phoneBrand=Meizu&platform=1&genderType=-1&topicId=19&phoneVersion=16&selectType=3&channel=MXMarket&phoneModel=M040&versionNumber=7.2.1&' % timestamp
    req = urllib2.Request(url, loadData, headers)
    loadResult = opener.open(req).read()
    loginStatus = json.loads(loadResult).get('status', False)
    if loginStatus == 1:
        print 'load successful!'
        timestamp, topicList = fetch_data(json.loads(loadResult))
        load(timestamp, headers, url)
    else:
        print 'load fail'
        print loadResult
        return False

loginUrl = 'http://120.55.151.61/V2/StudentSkip/loginCheckV4.action'
topicUrl = 'http://120.55.151.61/V2/Treehole/Message/getMessageByTopicIdV3.action'
classUrl = 'http://120.55.151.61/V2/Course/getCourseTableFromSchool3.action'
headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'User-Agent': 'Dalvik/1.6.0 (Linux; U; Android 4.1.1; M040 Build/JRO03H)',
    'Host': '120.55.151.61',
    'Connection': 'Keep-Alive',
    'Accept-Encoding': 'gzip',
    'Content-Length': '207',
    }

''' ---登录部分--- '''
loginData = 'phoneBrand=Meizu&platform=1&deviceCode=868033014919494&account=4B027B0DFA04DD9D7796FDC3A1282427&phoneVersion=16&password=BC892A42D928A7E026F147FC140C4300&channel=MXMarket&phoneModel=M040&versionNumber=7.2.1&'
cookieJar = CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
req = urllib2.Request(loginUrl, loginData, headers)
loginResult = opener.open(req).read()
loginStatus = json.loads(loginResult).get('data', False)
if loginResult:
    print 'login successful!'
else:
    print 'login fail'
    print loginResult

"""

''' ---获取话题--- '''
topicData = 'timestamp=0&phoneBrand=Meizu&platform=1&genderType=-1&topicId=19&phoneVersion=16&selectType=3&channel=MXMarket&phoneModel=M040&versionNumber=7.2.1&'
headers['Content-Length'] = '147'
topicRequest = urllib2.Request(topicUrl, topicData, headers)
topicHtml = opener.open(topicRequest).read()
topicJson = json.loads(topicHtml)
topicStatus = topicJson.get('status', False)
print topicJson
if topicStatus == 1:
    print 'fetch topic success!'
    timestamp, topicList = fetch_data(topicJson)
    load(timestamp, headers, topicUrl)
"""

# 这儿是获取课表
ClassData = ' phoneVersion=16&phoneBrand=Meizu&platform=1&parameters=%7B%22crawlType%22%3A0%2C%22hasVerCode%22%3Afalse%2C%22maxCount%22%3A0%2C%22parameterList%22%3A%5B%7B%22key%22%3A%22para1%22%2C%22question%22%3A0%2C%22type%22%3A1%2C%22value%22%3A%2279F07E42A30275BD1D1132BAE11C8408%22%7D%2C%7B%22key%22%3A%22para2%22%2C%22question%22%3A0%2C%22type%22%3A2%2C%22value%22%3A%2276D704007FF11CFD551D1B94662A2D66%22%7D%2C%7B%22key%22%3A%22pama3%22%2C%22question%22%3A0%2C%22type%22%3A3%2C%22value%22%3A%22bks%22%7D%5D%2C%22semester%22%3A%221%22%2C%22startSchoolYear%22%3A%222016%22%2C%22step%22%3A1%2C%22verCodeError%22%3Afalse%7D&channel=MXMarket&phoneModel=M040&versionNumber=7.2.1&'
headers['Content-Length'] = '667'
ClassRequest = urllib2.Request(classUrl, ClassData, headers)
ClassHtml = opener.open(ClassRequest).read()
ClassJson = json.loads(ClassHtml)
ClassStatus = ClassJson.get('status', False)
print ClassJson
posted @ 2016-06-22 01:40  qscqesze  阅读(1389)  评论(0编辑  收藏  举报