大多數(shù)APP里面返回的是json格式數(shù)據(jù),或者一堆加密過的數(shù)據(jù) 。這里以超級課程表APP為例,抓取超級課程表里用戶發(fā)的話題。
1、抓取APP數(shù)據(jù)包
表單:
表單中包括了用戶名和密碼,當(dāng)然都是加密過了的,還有一個(gè)設(shè)備信息,直接post過去就是。
另外必須加header,一開始我沒有加header得到的是登錄錯(cuò)誤,所以要帶上header信息。
2、登錄
登錄代碼:
import urllib2from cookielib import CookieJarloginUrl = ‘http://120.55.151.61/V2/StudentSkip/loginCheckV4.action’headers = {‘Content-Type’: ‘a(chǎn)pplication/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()print loginResult
登錄成功 會(huì)返回一串賬號信息的json數(shù)據(jù)
和抓包時(shí)返回?cái)?shù)據(jù)一樣,證明登錄成功
3、抓取數(shù)據(jù)
用同樣方法得到話題的url和post參數(shù)
下見最終代碼,有主頁獲取和下拉加載更新。可以無限加載話題內(nèi)容。
#!/usr/local/bin/python2.7# -*- coding: utf8 -*-“”“超級課程表話題抓取”“”import urllib2from cookielib import CookieJarimport json‘’‘ 讀Json數(shù)據(jù) ’‘’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&’ % timestampreq = 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 loadResultreturn FalseloginUrl =
‘http://120.55.151.61/V2/StudentSkip/loginCheckV4.action’topicUrl =
‘http://120.55.151.61/V2/Treehole/Message/getMessageByTopicIdV3.action’headers = {‘Content-Type’: ‘a(chǎn)pplication/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 topicJsonif topicStatus == 1:print ‘fetch topic success!’timestamp, topicList = fetch_data(topicJson)load(timestamp, headers, topicUrl)
結(jié)果:
責(zé)任編輯:haq
-
數(shù)據(jù)
+關(guān)注
關(guān)注
8文章
7080瀏覽量
89166 -
APP
+關(guān)注
關(guān)注
33文章
1574瀏覽量
72565 -
python
+關(guān)注
關(guān)注
56文章
4798瀏覽量
84805
原文標(biāo)題:利用Python爬蟲抓取手機(jī)APP的傳輸數(shù)據(jù)
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論