Python爬蟲(chóng)抓取手機(jī)APP的傳輸數(shù)據(jù)
大多數(shù)APP里面返回的是json格式數(shù)據(jù),或者一堆加密過(guò)的數(shù)據(jù) 。這里以超級(jí)課程表APP為例,抓取超級(jí)課程表里用戶發(fā)的話題。
1、抓取APP數(shù)據(jù)包
方法詳細(xì)可以參考這篇博文:Fiddler如何抓取手機(jī)APP數(shù)據(jù)包
得到超級(jí)課程表登錄的地址:http://120.55.151.61/V2/StudentSkip/loginCheckV4.action
表單:

表單中包括了用戶名和密碼,當(dāng)然都是加密過(guò)了的,還有一個(gè)設(shè)備信息,直接post過(guò)去就是。
另外必須加header,一開(kāi)始我沒(méi)有加header得到的是登錄錯(cuò)誤,所以要帶上header信息。

2、登錄
登錄代碼:
import urllib2
from cookielib import CookieJar
loginUrl = 'http://120.55.151.61/V2/StudentSkip/loginCheckV4.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()
print loginResult
登錄成功 會(huì)返回一串賬號(hào)信息的json數(shù)據(jù)

和抓包時(shí)返回?cái)?shù)據(jù)一樣,證明登錄成功

3、抓取數(shù)據(jù)
用同樣方法得到話題的url和post參數(shù)
做法就和模擬登錄網(wǎng)站一樣。詳見(jiàn):Python爬蟲(chóng)模擬登錄帶驗(yàn)證碼網(wǎng)站
下見(jiàn)最終代碼,有主頁(yè)獲取和下拉加載更新。可以無(wú)限加載話題內(nèi)容。
#!/usr/local/bin/python2.7
# -*- coding: utf8 -*-
"""
超級(jí)課程表話題抓取
"""
import urllib2
from cookielib import CookieJar
import 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&' % 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'
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)
load(timestamp, headers, topicUrl)
結(jié)果:

- Python實(shí)現(xiàn)爬取知乎神回復(fù)簡(jiǎn)單爬蟲(chóng)代碼分享
- 零基礎(chǔ)寫(xiě)python爬蟲(chóng)之爬蟲(chóng)編寫(xiě)全記錄
- Python爬蟲(chóng)框架Scrapy安裝使用步驟
- 零基礎(chǔ)寫(xiě)python爬蟲(chóng)之使用urllib2組件抓取網(wǎng)頁(yè)內(nèi)容
- 零基礎(chǔ)寫(xiě)python爬蟲(chóng)之使用Scrapy框架編寫(xiě)爬蟲(chóng)
- python抓取網(wǎng)頁(yè)圖片示例(python爬蟲(chóng))
- python制作爬蟲(chóng)并將抓取結(jié)果保存到excel中
- 零基礎(chǔ)寫(xiě)python爬蟲(chóng)之urllib2使用指南
- Python訪問(wèn)本地deepseek示例【含deepseek本地部署】
相關(guān)文章
Python??處理?Pandas?DataFrame?中的行和列
這篇文章主要介紹了Python處理Pandas?DataFrame中的行和列,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件)
這篇文章主要介紹了Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件) ,本文給大家介紹兩種方法,每種方法給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫(kù)教程
這篇文章主要介紹了Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫(kù),需要的朋友可以參考下2014-07-07
對(duì)Python中Iterator和Iterable的區(qū)別詳解
今天小編就為大家分享一篇對(duì)Python中Iterator和Iterable的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Spring @Enable模塊驅(qū)動(dòng)原理及使用實(shí)例
這篇文章主要介紹了Spring @Enable模塊驅(qū)動(dòng)原理及使用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

