Python crawler: Grab data from your mobile app

Source: Internet
Author: User

Summary: Most apps return JSON-formatted data, or a bunch of encrypted data. Take the Super Curriculum app as an example to grab the user's topic in the Super curriculum.

1. Crawl the app data package

The method can be consulted in detail in this blog post: http://my.oschina.net/jhao104/blog/605963

Get the Super Curriculum login address: http://120.55.151.61/V2/StudentSkip/loginCheckV4.action

Form:

The form includes the user name and password, of course, is encrypted, there is a device information, direct post in the past is.

Also must add the header, at first I did not add the header to get a login error, so bring the header information.

2. Login

Login code:

#Python Learning Exchange Group: 125240963ImportUrllib2 fromCookielibImportCookiejarloginurl='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 ()PrintLoginresult

Log on successfully returns a string of JSON data for the account information

The same as when the packet is returned, proving that the login was successful

3. Crawl Data

Get the URL and post parameters for the topic in the same way

It's just like impersonating a login site. See: http://my.oschina.net/jhao104/blog/547311

See below for the final code, with homepage fetching and drop-down loading updates. The topic content can be loaded indefinitely.

#!/usr/local/bin/python2.7#-*-Coding:utf8-*-"""Super Curriculum topic crawl"""ImportUrllib2 fromCookielibImportCookiejarImportJSON" "Read JSON data" "defFetch_data (json_data): Data= json_data['Data'] Timestamplong= data['Timestamplong'] Messagebo= data['Messagebos'] Topiclist= []     foreachinchmessagebo:topicdict= {}        ifEach.get ('content', False): topicdict['content'] = each['content'] topicdict['Schoolname'] = each['Schoolname'] topicdict['messageId'] = each['messageId'] topicdict['Gender'] = each['Studentbo']['Gender'] topicdict[' Time'] = each['Issuetime']            Printeach['Schoolname'],each['content'] Topiclist.append (topicdict)returnTimestamplong, Topiclist" "load More" "defload (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)ifLoginStatus = = 1:        Print 'Load successful!'timestamp, topiclist=Fetch_data (Json.loads (Loadresult)) load (timestamp, headers, URL)Else:        Print 'Load Fail'        PrintLoadresultreturnFalseloginurl='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',    }" "---login section---" "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)ifLoginresult:Print 'Login successful!'Else:    Print 'Login Fail'    PrintLoginresult" "---Get the topic---" "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)PrintTopicjsonifTopicstatus = = 1:    Print 'Fetch topic success!'timestamp, topiclist=fetch_data (Topicjson) load (timestamp, headers, topicurl)

Results:

Python Learning Exchange Group: 125240963

Reprint please indicate source: http://my.oschina.net/jhao104/blog/606922

Python crawler: Grab data from your mobile app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.