python 抓取 摩拜单车 数据demo

数据采集,搜索推荐

2017-12-09

37

0

技术:python2.7 requests

运行环境:win10 + python2.7

demo功能:抓取摩拜单车的单车数据

源代码

没啥好说的, 直接贴代码 

#!/usr/bin/env python
# coding: utf-8
import requests
import numpy as np
import json

#这里以上海为区域为准 查询单车
def start():
    left = 31.5053961647
    right = 30.7049518197

    top = 121.0347223528
    bottom = 121.8421671948
    total = 0
    offset = 0.002

    lat_range = np.arange(left, right, -offset)
    for lat in lat_range:
        lon_range = np.arange(top, bottom, offset)
        for lon in lon_range:
            total += 1
            # print '纬度=' + str(lat) + ',经度=' + str(lon)
            get_nearby_bikes(lat, lon)
            # print '当前共=' + str(total)

#api 抓包小程序吧
def get_nearby_bikes(lat, lon):
    url = "https://mwx.mobike.com/mobike-api/rent/nearbyBikesInfo.do"
    payload = "latitude=%s&longitude=%s&errMsg=getMapCenterLocation" % (lat,
                                                                        lon)
    headers = {
        'charset': "utf-8",
        'platform': "4",
        "referer": "https://servicewechat.com/wx40f112341ae33edb/1/",
        'content-type': "application/x-www-form-urlencoded",
        'user-agent': "MicroMessenger/6.5.4.1000 NetType/WIFI Language/zh_CN",
        'host': "mwx.mobike.com",
        'connection': "Keep-Alive",
        'accept-encoding': "gzip",
        'cache-control': "no-cache"
    }
    request(headers, payload, url)


def request(headers, payload, url):
    response = requests.request(
        "POST", url, data=payload, headers=headers, timeout=5, verify=False)
    json_obj = json.loads(response.text)
    if len(json_obj['object']) > 0:
        print json_obj['object']


if __name__ == '__main__':
    start()
 
 

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think