來源:Python程式設計
ID:LovePython
大多數APP裡面傳回的是json格式資料,或者一堆加密過的資料 。這裡以超級課程表APP為例,抓取超級課程表裡使用者發的話題。
方法詳細可以參考這篇博文:http://my.oschina.net/jhao104/blog/605963
得到超級課程表登入的地址:http://120.55.151.61/V2/StudentSkip/loginCheckV4.action
表單:
表單中包括了使用者名稱和密碼,當然都是加密過了的,還有一個裝置資訊,直接post過去就是。
另外必須加essay-header,一開始我沒有加essay-header得到的是登入錯誤,所以要帶上essay-header資訊。
登入程式碼:
import urllib2
from cookielib import CookieJar
loginUrl = 'http://120.55.151.61/V2/StudentSkip/loginCheckV4.action' essay-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, essay-headers)
loginResult = opener.open(req).read()
print loginResult
登入成功 會傳回一串賬號資訊的json資料
和抓包時傳回資料一樣,證明登入成功
用同樣方法得到話題的url和post引數
做法就和模擬登入網站一樣。詳見:http://my.oschina.net/jhao104/blog/547311
下見最終程式碼,有主頁獲取和下拉載入更新。可以無限載入話題內容。
#!/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, essay-headers, url):
essay-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, essay-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, essay-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' essay-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, essay-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&'
essay-headers['Content-Length'] = '147'
topicRequest = urllib2.Request(topicUrl, topicData, essay-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, essay-headers, topicUrl)
結果:
來自:j_hao104的個人頁面
連結:https://my.oschina.net/jhao104/blog/606922(點選尾部閱讀原文前往)
《Python人工智慧和全棧開發》2018年07月23日即將在北京開課,120天衝擊Python年薪30萬,改變速約~~~~
*宣告:推送內容及圖片來源於網路,部分內容會有所改動,版權歸原作者所有,如來源資訊有誤或侵犯權益,請聯絡我們刪除或授權事宜。
– END –
更多Python好文請點選【閱讀原文】哦
↓↓↓