python 으로 모 바 이 자전거 API 데 이 터 를 캡 처 합 니 다.

요즘 모 바 이 자전거 가 엄 청 뜨 거 워 서 퇴근 하고 여러 가지 타고 무릎 을 다 쳤 어 요.................................................
-----------------------------------------------------------------------
이 문장 을 보면http://mp.weixin.qq.com/s?__biz=MzA5NDExMTAzNA==&mid=2649982414&idx=1&sn=68b638c4f019baa3a783c045b294d6de&chksm=8854b19bbf23388dd91509a2a692736a5c0505ab144c5605d4015330044699c20e5c42f92496&mpshare=1&scene=1&srcid=0519Fkgd6ew4TuOHUerHMlZs#rd
원본 주기 로 했 는데 안 준 것 같은 데...
그리고 혼자 썼어 요.
--------------------------------------------------------------------------------------
import requests
import numpy as np
import json
import multiprocessing
from requests.packages.urllib3.exceptions import InsecureRequestWarning
#
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

#
PROCESS_NUM = 4
#
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'
}
#
LEFT = 28.026735
TOP = 120.644784
RIGHT = 27.99344
BOTTOM = 120.715068
INIT_POS = (TOP, LEFT, BOTTOM, RIGHT)

# num ,
def split_pos(init_pos, num):
result = []
x1 = init_pos[0]
y1 = init_pos[1]
x2 = init_pos[2]
y2 = init_pos[3]
xrange = np.linspace(x1, x2, num + 1)
for i in range(num):
# ,
result.append((xrange[i], y1, xrange[i+1], y2))
return result


#
# MONGDB , 。。
def save(text, output):
try:
json_data = json.loads(text)
except:
print('***ERR1*** ', text)
return
if 'object' not in json_data:
print('***ERR2*** ', text)
return
for object in json_data['object']:
obj_values = []
for key in sorted(object.keys()):
try:
if(object[key] == None):
obj_values.append('')
else:
obj_values.append(str(object[key]))
except:
print('***ERR3*** ', object)
return
output.write('$'.join(obj_values) + '
')


# , , IP 、
# 。。
def worker(num, location):
raw_fd = open('worker' + str(num) + '_rawdata.txt', 'w')
tabular_fd = open('worker' + str(num) + '_tabulardata.txt', 'w')
session = requests.session()
url = 'https://mwx.mobike.com/mobike-api/rent/nearbyBikesInfo.do'
#
postdata = {
'latitude': 0,
'longitude': 0,
'errMsg': 'getMapCenterLocation'
}
(top, left, bottom, right) = location
# ,
lat_range = np.arange(left, right, -0.002)
for lat in lat_range:
lon_range = np.arange(top, bottom, 0.002)
for lon in lon_range:
postdata['latitude'] = lat
postdata['longitude'] = lon
mobike_data = session.post(url, headers=HEADERS, data=postdata, verify=False)
#print(mobike_data.text)
# , ,
raw_fd.write(mobike_data.text + '
')
# , , ,
save(mobike_data.text, tabular_fd)
raw_fd.close()
tabular_fd.close()

#
if __name__ == '__main__':
jobs = []
location = split_pos(INIT_POS, PROCESS_NUM)
# ,
for process_id in range(PROCESS_NUM):
p = multiprocessing.Process(target=worker, args=(process_id,location[process_id]))
jobs.append(p)
p.start()

다음으로 전송:https://www.cnblogs.com/kernel521/p/6894443.html

좋은 웹페이지 즐겨찾기