利用python爬取mobike在西安地区的分布情况

先附上整个思路流程
1,获取mobike单车的API接口
2,爬取API数据
3,数据可视化
一:利用Fiddle抓取mobileAPI数据接口得到post请求如下
POST https://mwx.mobike.com/nearby/nearbyBikeInfo HTTP/1.1
charset: utf-8
Accept-Encoding: gzip
opensrc: list
eption: 0a235
time: 时间戳
subsource:
longitude:
mobileno:
content-type: application/x-www-form-urlencoded
referer: https://servicewechat.com/wx80f809371ae33eda/350/page-frame.html
mainsource: 4003
userid: 4084302776004076639232125813
platform: 4
wxapp: 1
lang: zh
latitude: 34.141276
citycode: 029
accesstoken: 941e812e4db1652fd753af5a81242ac4-0
locationtime: 1540817570095
User-Agent: Mozilla/5.0 (Linux; Android 7.1.2; Redmi 4X Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36 MicroMessenger/6.7.3.1360(0x26070336) NetType/WIFI Language/zh_CN Process/appbrand3
Content-Length: 184
Host: mwx.mobike.com
Connection: Keep-Alive
biketype=0&speed=0&errMsg=getLocation%3Aok&longitude=&verticalAccuracy=0&latitude=&accuracy=30&horizontalAccuracy=30&userid=4084302776004076639232125813&citycode=029
##二:分析链接
POST https://mwx.mobike.com/nearby/nearbyBikeInfo HTTP/1.1
charset: utf-8
Accept-Encoding: gzip
opensrc: list
eption: 0a235
time: 这里为当前时间戳
subsource:
longitude: x坐标以百度坐标系为准
mobileno:mobike账号
content-type: application/x-www-form-urlencoded
referer: https://servicewechat.com/wx80f809371ae33eda/350/page-frame.html
mainsource: 4003
userid: 4084302776004076639232125813
platform: 4
wxapp: 1
lang: zh
latitude: y坐标以百度坐标系为准
citycode: 029
accesstoken: 941e812e4db1652fd753af5a81242ac4-0
locationtime: 1540817570095
User-Agent: Mozilla/5.0 (Linux; Android 7.1.2; Redmi 4X Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36 MicroMessenger/6.7.3.1360(0x26070336) NetType/WIFI Language/zh_CN Process/appbrand3
Content-Length: 184
Host: mwx.mobike.com
Connection: Keep-Alive
biketype=0&speed=0&errMsg=getLocation%3Aok&longitude=&verticalAccuracy=0&latitude=&accuracy=30&horizontalAccuracy=30&userid=4084302776004076639232125813&citycode=029
##三:通过更改headers伪装成手机浏览器以获得返回数据
这里就不细说,简单的两个for循环
##四:数据可视化,利用百度地图提供的API进行热度分析
首先获得百度开发者的ak,然后就百度去吧。。。。。。
##代码(一):

import requests
import time
import re

def mobike(x,y):
    t = time.time()
    num = int(t)
    num = str(num)
    num1=str(x)
    num2=str(y)
    url = 'https://mwx.mobike.com/nearby/nearbyBikeInfo'
    doc = {'charset': 'utf-8', 'Accept-Encoding': 'gzip', 'opensrc': 'list', 'eption': '0a235', 'time': num,
           'subsource': '', 'longitude': num1, 'mobileno': '========',忽略账号
           'content-type': 'application/x-www-form-urlencoded',
           'referer': 'https://servicewechat.com/wx80f809371ae33eda/350/page-frame.html', 'mainsource': '4003',
           'userid': '4084302776004076639232125813', 'platform': '4', 'wxapp': '1', 'lang': 'zh',
           'latitude': num2, 'citycode': '029', 'accesstoken': '941e812e4db1652fd753af5a81242ac4-0',
           'locationtime': '1540817570095',
           'User-Agent': 'Mozilla/5.0 (Linux; Android 7.1.2; Redmi 4X Build/N2G47H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.158 Mobile Safari/537.36 MicroMessenger/6.7.3.1360(0x26070336) NetType/WIFI Language/zh_CN Process/appbrand3',
           'Content-Length': '184', 'Host': 'mwx.mobike.com', 'Connection': 'Keep-Alive'}
    doc1 = (
        'biketype=0&speed=0&errMsg=getLocation%3Aok&longitude='+num1+'&verticalAccuracy=0&latitude='+num2+'&accuracy=30&horizontalAccuracy=30&userid=4084302776004076639232125813&citycode=029').encode('ascii')
    requests.packages.urllib3.disable_warnings()
    r = requests.post(url, data=doc1, headers=doc, verify=False)
    doc3 = r.text
    return doc3
num=[]
num1=[]
num2=[]
num3=[]
a=0
b=0
xlabel=108.852739
ylabel=34.194159
for x in range(600):
    xlabel = xlabel + 0.001
    for y in range(600):
        b+=1
        print('\r{0:2f}%'.format(b/412),end='')
        ylabel=ylabel+0.001
        doc = mobike(xlabel, ylabel)
        doc1 = re.findall(r'distX":\S*?,', doc)
        doc2 = re.findall(r'distY":\S*?,', doc)
        doc3 = re.findall(r'distance":"\S*?"', doc)
        doc4 = re.findall(r'"bikeIds":"\S*?"', doc)
        for tag in doc1:
            a += 1
            tag = tag.replace('distX":', '')
            tag = tag.replace(',', '')
            num.append(tag)
        for tag in doc2:
            tag = tag.replace('distY":', '')
            tag = tag.replace(',', '')
            num1.append(tag)
        for tag in doc3:
            tag = tag.replace('distance":"', '')
            tag = tag.replace('"', '')
            num2.append(tag)
        for tag in doc4:
            tag = tag.replace('"bikeIds":"', '')
            tag = tag.replace('"', '')
            num3.append(tag)
        print(num,num1,num2,num3)
        for i in range(a):
            fd = open('C:/file/mobike/bikex.txt', 'a+')
            fd.write(num[i]+','+num1[i]+','+num2[i]+','+num3[i]+'\n')
        num.clear()
        num1.clear()
        num2.clear()
        num3.clear()
        a=0
    ylabel = 34.194159

##结果(一)
108.8538328,34.19527291,15,0296529592#分别为x,y,距离,车辆id
108.85350429,34.19522746,22,8630345501#
108.85341442,34.19527363,32,8631006108#
108.85409442,34.1953775,40,8630612397#
108.8532906,34.19521682,41,8630905414#
108.8535073,34.19549054,42,8618761638#
108.85325267,34.19528091,46,8630087118#
108.85431808,34.19521107,53,0296535968#
108.85435901,34.19516798,57,8630788171#
108.85342237,34.19471543,57,8630803170#
108.85396464,34.19564681,58,8630674508#
108.85318478,34.19544408,60,8630189207#
108.85440096,34.19529896,62,0296518237#
108.85435404,34.19545009,65,8630036024#
108.85336744,34.19465751,65,8630033074#
108.85342345,34.19573777,70,0290036159#
108.85457573,34.19552874,87,8630499315#
108.85316084,34.19575123,84,8630391691#
108.85368408,34.19598341,91,8630282224#
108.85312989,34.1958193,92,0296537075#
##可视化代码:

    





    
    
    
    
    热力图功能示例
    


	

##结果展示

你可能感兴趣的:(python)