加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python爬虫-抓取手机APP数据

发布时间:2020-12-17 17:29:25 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 抓取超级课程表话题数据。 博文:http://my.oschina.net/jhao104/blog/606922 #!/usr/local/bin/python2.7# -*- coding: utf8 -*-""" 超级课程表话题

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

抓取超级课程表话题数据。
博文:http://my.oschina.net/jhao104/blog/606922
#!/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,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'
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=FCF030E1F2F6341C1C93BE5BBC422A3D&phoneVersion=16&password=A55B48BB75C79200379D82A18C5F47D6&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)
    data = load(timestamp,topicUrl)
    if data:
        timestamp,topicList = fetch_data(data)

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读