先前参考了其余的代码,大多数是python2.7写的,而3.6用的类库以及规则有了很大的变更,因此本身写了一个这样的代码,供给你们参考。python
1 def get_station(i): 2 station=[] 3 bus_sto=urllib.parse.quote(u'公交站'.encode('utf-8')) 4 city=urllib.parse.quote(u'南昌'.encode('utf-8')) 5 url1="http://api.map.baidu.com/place/v2/search?ak=&output=json&query=%s&scope=1®ion=%s&page_size=1&page_num=%d"%(bus_sto,city,i) 6 #encodedStr = urllib.parse.quote(url1, safe="/:=&?#+!$,;'@()*[]") 7 req=requests.get(url1) 8 content= req.content 9 data=json.loads(content) 10 station.append(data['total']) 11 result=data['results'] 12 str_temp=result[0] 13 loc=str_temp['location'] 14 lng=float(loc['lng']) 15 lat=float(loc['lat']) 16 station.append(str_temp['name']+",%f"%lng+",%f"%lat) 17 print (station[1]) 18 return station 19 20 def run(): 21 get_num=get_station(0) 22 num=get_num[0] 23 for i in range(0,num): 24 Bstation=get_station(i) 25 file = open('.\\baidull.txt','a') 26 file.writelines(Bstation[1]+'\n') 27 file.close() 28 29 if __name__=='__main__': 30 run()
问题仍是有一些,好比说调用API时返回的数据有时不足,应该是百度地图那边作了必定的限制,后续可能会采用随机IP的方法。json