import requests address = ‘40.8587960,86.866991‘ url = ‘http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=‘ + str(address) response = requests.get(url) answer = response.json() print(‘得到反解数据‘, answer)
使用python爬虫
1 import requests 2 address = ‘40.8587960,86.866991‘ 3 url = ‘http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=‘ + str(address) 4 response = requests.get(url) 5 answer = response.json() 6 print(‘得到反解数据‘, answer) 7 lng = answer[‘result‘][‘location‘][‘lng‘] 8 lat = answer[‘result‘][‘location‘][‘lat‘] 9 formatted_address = answer[‘result‘][‘formatted_address‘] 10 business = answer[‘result‘][‘business‘] 11 city = answer[‘result‘][‘addressComponent‘][‘city‘] 12 direction = answer[‘result‘][‘addressComponent‘][‘city‘] 13 distance = answer[‘result‘][‘addressComponent‘][‘direction‘] 14 district = answer[‘result‘][‘addressComponent‘][‘district‘] 15 province = answer[‘result‘][‘addressComponent‘][‘province‘] 16 street = answer[‘result‘][‘addressComponent‘][‘street‘] 17 street_number = answer[‘result‘][‘addressComponent‘][‘street_number‘] 18 cityCode = answer[‘result‘][‘cityCode‘] 19 s = str(lng) + ‘|‘ + str(lat)+ ‘|‘ + str(formatted_address) + ‘|‘ + str(business) + ‘|‘+ str(city) + ‘|‘+ str(direction) + ‘|‘+ str(distance) + ‘|‘+ str(district) + ‘|‘+ str(province) + ‘|‘+ str(street) + ‘|‘+ str(street_number) + ‘|‘+ str(cityCode) 20 print(s)
解析数据
http://api.map.baidu.com/lbsapi/getpoint/index.html
1 #鸡头1 2 #129.000000,54.000000 #锚点(鸡头) 3 #115.000000,54.0000000 4 #115.000000,42.0000000 #北京 5 #129.000000,42.000000 #沈阳 6 #[‘54.0000000‘, ‘42.000000‘, ‘129.000000‘, ‘115.000000‘],#鸡头1 7 8 #鸡头2 9 #129.000000,42.000000 #沈阳 10 #135.200000,42.000000 11 #129.000000,50.000000 12 #135.200000,50.000000 13 #[‘50.000000‘,‘42.000000‘,‘135.000000‘,‘129.000000‘],#鸡头2 14 #鸡头补充 15 #115.000000,42.0000000 #北京 16 #105.0000000,42.000000 #锚点(银川上面) 17 #105.0000000,45.500000 #乌兰巴托 18 #115.000000,45.500000 19 #[‘45.500000‘, ‘42.000000‘, ‘115.000000‘, ‘105.000000‘],#鸡头补充 20 21 #鸡屁股上补充 22 #105.0000000,42.000000 #锚点(银川上面) 23 #91.500000,45.500000 #乌鲁木齐 24 #105.0000000,45.500000 #乌兰巴托 25 #91.500000,42.000000 #锚点(鸡屁股) 26 #[‘45.500000‘, ‘42.000000‘, ‘105.0000000‘, ‘91.500000‘],#鸡屁股上补充 27 #鸡屁股上 28 #91.500000,42.000000 #锚点(鸡屁股) 29 #79.800000,42.000000 #啊拉木图 30 #79.800000,49.200000 31 #91.500000,49.200000 32 #[‘49.200000‘, ‘42.000000‘, ‘91.500000‘, ‘79.800000‘],#鸡屁股上 33 #鸡屁股下 34 #79.800000,42.000000 #啊拉木图 35 #79.800000,30.0000000 #巴雷利 36 #73.400000,42.000000 37 #73.400000,30.000000 38 #[‘42.000000‘, ‘30.000000‘, ‘79.800000‘, ‘73.400000‘],#鸡屁股下 39 #鸡下补充 40 #79.800000,30.0000000 #巴雷利 41 #97.300000,30.000000 #拉萨昌都(锚点) 42 #97.300000,26.800000 43 #79.800000,26.800000 44 #[‘42.000000‘, ‘30.000000‘, ‘97.300000‘, ‘79.800000‘],#鸡下补充 45 #鸡下 46 #97.300000,30.000000 #拉萨昌都(锚点) 47 #105.000000,30.000000 #(成都重庆贵阳) 48 #105.000000,21.000000 #河内 49 #97.300000,21.000000 #曼德勒 50 #[‘30.000000‘, ‘21.000000‘, ‘105.000000‘, ‘97.300000‘],#鸡下 51 52 53 #鸡胸 54 #105.0000000,42.000000 #锚点(银川上面) 55 #105.0000000,21.000000 #河内 56 #129.000000,42.000000 #沈阳 57 #129.000000,21.000000 #台北(海) 58 #[‘42.000000‘, ‘21.000000‘, ‘129.000000‘, ‘105.000000‘],#鸡胸 59 60 #鸡身 61 #105.000000,42.000000 #锚点(银川上面) 62 #79.800000,42.000000 #啊拉木图 63 #79.800000,30.000000 #巴雷利 64 #105.000000,30.000000 #(成都重庆贵阳) 65 #[‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘],#鸡身 66 #鸡脚 (南海) 67 #105.000000,21.000000 #河内 68 #129.000000,21.000000 #台北(海) 69 #105.0000000,3.000000 #新加坡 70 #129.000000,3.000000 #海(苏拉威西海) 71 #[‘21.000000‘, ‘3.000000‘, ‘129.000000‘, ‘105.000000‘],
经纬度分析
1 import requests 2 from decimal import Decimal 3 def obtain(address): 4 url = ‘http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=‘ + str(address) 5 response = requests.get(url) 6 answer = response.json() 7 print(‘得到反解数据‘, answer) 8 lng = answer[‘result‘][‘location‘][‘lng‘] 9 lat = answer[‘result‘][‘location‘][‘lat‘] 10 formatted_address = answer[‘result‘][‘formatted_address‘] 11 business = answer[‘result‘][‘business‘] 12 city = answer[‘result‘][‘addressComponent‘][‘city‘] 13 direction = answer[‘result‘][‘addressComponent‘][‘city‘] 14 distance = answer[‘result‘][‘addressComponent‘][‘direction‘] 15 district = answer[‘result‘][‘addressComponent‘][‘district‘] 16 province = answer[‘result‘][‘addressComponent‘][‘province‘] 17 street = answer[‘result‘][‘addressComponent‘][‘street‘] 18 street_number = answer[‘result‘][‘addressComponent‘][‘street_number‘] 19 cityCode = answer[‘result‘][‘cityCode‘] 20 s = str(‘%6f‘ % lng) + ‘|‘ + str(‘%6f‘ % lat) + ‘|‘ + str(formatted_address) + ‘|‘ + str(business) + ‘|‘ + str( 21 city) + ‘|‘ + str(direction) + ‘|‘ + str(distance) + ‘|‘ + str(district) + ‘|‘ + str(province) + ‘|‘ + str( 22 street) + ‘|‘ + str(street_number) + ‘|‘ + str(cityCode) 23 print(s) 24 25 def longitude(slon,precision,elon,slat): 26 while slon >= elon: 27 address = ‘%s,%s‘ % (slon,slat) # 请求用的,经度,纬度, 28 obtain(address)#传入经度,纬度,获取地址地名 29 slon -= precision 30 31 #[‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 32 slon = Decimal(‘42.000000‘)#经度longitude开始 33 elon = Decimal(‘30.000000‘)#经度结束 34 slat=Decimal(‘105.000000‘)# 纬度latitude开始 35 elat=Decimal(‘79.800000‘)#纬度结束 36 precision = Decimal(‘5.0001000‘)#精度 0.0001000==1000米 37 while slat>=elat: 38 slat -= precision 39 longitude(slon,precision,elon,slat)
根据精度获取多个地名
1 import requests 2 from decimal import Decimal 3 def obtain(address): 4 url = ‘http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=‘ + str(address) 5 response = requests.get(url) 6 answer = response.json() 7 print(‘得到反解数据‘, answer) 8 lng = answer[‘result‘][‘location‘][‘lng‘] 9 lat = answer[‘result‘][‘location‘][‘lat‘] 10 formatted_address = answer[‘result‘][‘formatted_address‘] 11 business = answer[‘result‘][‘business‘] 12 city = answer[‘result‘][‘addressComponent‘][‘city‘] 13 direction = answer[‘result‘][‘addressComponent‘][‘city‘] 14 distance = answer[‘result‘][‘addressComponent‘][‘direction‘] 15 district = answer[‘result‘][‘addressComponent‘][‘district‘] 16 province = answer[‘result‘][‘addressComponent‘][‘province‘] 17 street = answer[‘result‘][‘addressComponent‘][‘street‘] 18 street_number = answer[‘result‘][‘addressComponent‘][‘street_number‘] 19 cityCode = answer[‘result‘][‘cityCode‘] 20 s = str(‘%6f‘ % lng) + ‘|‘ + str(‘%6f‘ % lat) + ‘|‘ + str(formatted_address) + ‘|‘ + str(business) + ‘|‘ + str( 21 city) + ‘|‘ + str(direction) + ‘|‘ + str(distance) + ‘|‘ + str(district) + ‘|‘ + str(province) + ‘|‘ + str( 22 street) + ‘|‘ + str(street_number) + ‘|‘ + str(cityCode) 23 print(s) 24 25 def longitude(slon,precision,elon,slat): 26 while slon >= elon: 27 address = ‘%s,%s‘ % (slon,slat) # 请求用的,经度,纬度, 28 obtain(address)#传入经度,纬度,获取地址地名 29 slon -= precision 30 31 LogLatList=[ 32 [‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 33 [‘42.000000‘, ‘21.000000‘, ‘129.000000‘, ‘105.000000‘], # 鸡胸 34 [‘50.000000‘, ‘42.000000‘, ‘135.000000‘,‘129.000000‘], #鸡头2 35 [‘54.000000‘, ‘42.000000‘, ‘129.000000‘, ‘115.000000‘], #鸡头1 36 [‘45.500000‘, ‘42.000000‘, ‘115.000000‘, ‘105.000000‘], #鸡头补充 37 [‘49.200000‘, ‘42.000000‘, ‘91.500000‘, ‘79.800000‘], # 鸡屁股上 38 [‘42.000000‘, ‘30.000000‘, ‘79.800000‘, ‘73.400000‘], # 鸡屁股下 39 [‘45.500000‘, ‘42.000000‘, ‘105.000000‘, ‘91.500000‘], #鸡屁股上补充 40 [‘30.000000‘, ‘21.000000‘, ‘105.000000‘, ‘97.300000‘], # 鸡下 41 [‘42.000000‘, ‘30.000000‘, ‘97.300000‘, ‘79.800000‘], #鸡下补充 42 [‘21.000000‘, ‘3.000000‘, ‘129.000000‘, ‘105.000000‘], # 鸡脚 (南海) 43 ] 44 precision = Decimal(‘10.0001000‘) # 精度 0.0001000==1000米 45 for single_list in LogLatList: 46 slons,elons,slats,elats=single_list 47 #[‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 48 slon = Decimal(slons)#经度longitude开始 49 elon = Decimal(elons)#经度结束 50 slat=Decimal(slats)# 纬度latitude开始 51 elat=Decimal(elats)#纬度结束 52 53 while slat>=elat: 54 slat -= precision 55 longitude(slon,precision,elon,slat)
批量获取数据
1 import requests 2 from decimal import Decimal 3 import threading , time 4 def obtain(address): 5 url = ‘http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=‘ + str(address) 6 response = requests.get(url) 7 answer = response.json() 8 print(‘得到反解数据‘, answer) 9 lng = answer[‘result‘][‘location‘][‘lng‘] 10 lat = answer[‘result‘][‘location‘][‘lat‘] 11 formatted_address = answer[‘result‘][‘formatted_address‘] 12 business = answer[‘result‘][‘business‘] 13 city = answer[‘result‘][‘addressComponent‘][‘city‘] 14 direction = answer[‘result‘][‘addressComponent‘][‘city‘] 15 distance = answer[‘result‘][‘addressComponent‘][‘direction‘] 16 district = answer[‘result‘][‘addressComponent‘][‘district‘] 17 province = answer[‘result‘][‘addressComponent‘][‘province‘] 18 street = answer[‘result‘][‘addressComponent‘][‘street‘] 19 street_number = answer[‘result‘][‘addressComponent‘][‘street_number‘] 20 cityCode = answer[‘result‘][‘cityCode‘] 21 s = str(‘%6f‘ % lng) + ‘|‘ + str(‘%6f‘ % lat) + ‘|‘ + str(formatted_address) + ‘|‘ + str(business) + ‘|‘ + str( 22 city) + ‘|‘ + str(direction) + ‘|‘ + str(distance) + ‘|‘ + str(district) + ‘|‘ + str(province) + ‘|‘ + str( 23 street) + ‘|‘ + str(street_number) + ‘|‘ + str(cityCode) 24 print(s) 25 26 def longitude(slon,precision,elon,slat): 27 while slon >= elon: 28 address = ‘%s,%s‘ % (slon,slat) # 请求用的,经度,纬度, 29 obtain(address)#传入经度,纬度,获取地址地名 30 slon -= precision 31 32 33 class Thre(threading.Thread):#继承线程中的类 34 #Thre(single_list,precision,ctime) 35 def __init__(self,single_list,precision,ctime): 36 super(Thre,self).__init__()#重新写父类,解决多继承问题 37 self.precision=precision#精度 38 self.single_list=single_list#单列表 39 self.ctime=ctime#当前时间 40 def run(self): 41 print(‘执行线程开始时间:‘, self.ctime, ‘执行的列表:‘,self.single_list) 42 slons, elons, slats, elats = self.single_list 43 # [‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 44 slon = Decimal(slons) # 经度longitude开始 45 elon = Decimal(elons) # 经度结束 46 slat = Decimal(slats) # 纬度latitude开始 47 elat = Decimal(elats) # 纬度结束 48 while slat >= elat: 49 slat -= self.precision 50 longitude(slon,self.precision, elon, slat) 51 else: 52 end_time=time.time()-self.ctime 53 print(‘执行线程所用时间:‘,end_time, ‘执行的列表:‘,self.single_list) 54 55 def main(): 56 LogLatList=[ 57 [‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 58 [‘42.000000‘, ‘21.000000‘, ‘129.000000‘, ‘105.000000‘], # 鸡胸 59 [‘50.000000‘, ‘42.000000‘, ‘135.000000‘,‘129.000000‘], #鸡头2 60 [‘54.000000‘, ‘42.000000‘, ‘129.000000‘, ‘115.000000‘], #鸡头1 61 [‘45.500000‘, ‘42.000000‘, ‘115.000000‘, ‘105.000000‘], #鸡头补充 62 [‘49.200000‘, ‘42.000000‘, ‘91.500000‘, ‘79.800000‘], # 鸡屁股上 63 [‘42.000000‘, ‘30.000000‘, ‘79.800000‘, ‘73.400000‘], # 鸡屁股下 64 [‘45.500000‘, ‘42.000000‘, ‘105.000000‘, ‘91.500000‘], #鸡屁股上补充 65 [‘30.000000‘, ‘21.000000‘, ‘105.000000‘, ‘97.300000‘], # 鸡下 66 [‘42.000000‘, ‘30.000000‘, ‘97.300000‘, ‘79.800000‘], #鸡下补充 67 [‘21.000000‘, ‘3.000000‘, ‘129.000000‘, ‘105.000000‘], # 鸡脚 (南海) 68 ] 69 precision = Decimal(‘20.0001000‘) # 精度 0.0001000==1000米 70 71 thre_list=[] #线程列表 72 for single_list in LogLatList: 73 ctime=time.time()#当前时间 74 temp=Thre(single_list,precision,ctime)#实例化类 75 thre_list.append(temp)#线程列表 76 for thre in thre_list:#线程列表 77 thre.start()#执行单个线程 78 79 if __name__ == ‘__main__‘: 80 main()
多线程获取数据
1 import requests 2 from decimal import Decimal 3 import threading , time 4 def obtain(address,file_name): 5 url = ‘http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=‘ + str(address) 6 response = requests.get(url) 7 answer = response.json() 8 print(‘得到反解数据‘, answer) 9 lng = answer[‘result‘][‘location‘][‘lng‘] 10 lat = answer[‘result‘][‘location‘][‘lat‘] 11 formatted_address = answer[‘result‘][‘formatted_address‘] 12 business = answer[‘result‘][‘business‘] 13 city = answer[‘result‘][‘addressComponent‘][‘city‘] 14 direction = answer[‘result‘][‘addressComponent‘][‘city‘] 15 distance = answer[‘result‘][‘addressComponent‘][‘direction‘] 16 district = answer[‘result‘][‘addressComponent‘][‘district‘] 17 province = answer[‘result‘][‘addressComponent‘][‘province‘] 18 street = answer[‘result‘][‘addressComponent‘][‘street‘] 19 street_number = answer[‘result‘][‘addressComponent‘][‘street_number‘] 20 cityCode = answer[‘result‘][‘cityCode‘] 21 s = str(‘%6f‘ % lng) + ‘|‘ + str(‘%6f‘ % lat) + ‘|‘ + str(formatted_address) + ‘|‘ + str(business) + ‘|‘ + str( 22 city) + ‘|‘ + str(direction) + ‘|‘ + str(distance) + ‘|‘ + str(district) + ‘|‘ + str(province) + ‘|‘ + str( 23 street) + ‘|‘ + str(street_number) + ‘|‘ + str(cityCode) 24 print(s) 25 if cityCode==0: 26 #print(‘外国‘) 27 pass 28 else: 29 #print(s) 30 fname=str(file_name)+‘list‘ 31 with open(fname, ‘a+‘, encoding=(‘utf-8‘)) as f: 32 f.write(s+‘\n‘) 33 34 def longitude(slon,precision,elon,slat,file_name): 35 while slon >= elon: 36 address = ‘%s,%s‘ % (slon,slat) # 请求用的,经度,纬度, 37 obtain(address,file_name)#传入经度,纬度,获取地址地名 38 slon -= precision 39 40 41 class Thre(threading.Thread):#继承线程中的类 42 #Thre(single_list,precision,ctime) 43 #temp = Thre(single_list, precision, ctime, file_name) # 实例化类 44 def __init__(self,single_list,precision,ctime,file_name): 45 super(Thre,self).__init__()#重新写父类,解决多继承问题 46 self.precision=precision#精度 47 self.single_list=single_list#单列表 48 self.ctime=ctime#当前时间 49 self.file_name=file_name#文件名 50 def run(self): 51 print(‘执行线程开始时间:‘, self.ctime, ‘执行的列表:‘,self.single_list) 52 slons, elons, slats, elats = self.single_list 53 # [‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 54 slon = Decimal(slons) # 经度longitude开始 55 elon = Decimal(elons) # 经度结束 56 slat = Decimal(slats) # 纬度latitude开始 57 elat = Decimal(elats) # 纬度结束 58 while slat >= elat: 59 slat -= self.precision 60 longitude(slon,self.precision, elon, slat,self.file_name) 61 else: 62 end_time=time.time()-self.ctime 63 print(‘执行线程所用时间:‘,end_time, ‘执行的列表:‘,self.single_list) 64 65 def main(): 66 LogLatList=[ 67 [‘42.000000‘, ‘30.000000‘, ‘105.000000‘, ‘79.800000‘], # 鸡身 68 [‘42.000000‘, ‘21.000000‘, ‘129.000000‘, ‘105.000000‘], # 鸡胸 69 [‘50.000000‘, ‘42.000000‘, ‘135.000000‘,‘129.000000‘], #鸡头2 70 [‘54.000000‘, ‘42.000000‘, ‘129.000000‘, ‘115.000000‘], #鸡头1 71 [‘45.500000‘, ‘42.000000‘, ‘115.000000‘, ‘105.000000‘], #鸡头补充 72 [‘49.200000‘, ‘42.000000‘, ‘91.500000‘, ‘79.800000‘], # 鸡屁股上 73 [‘42.000000‘, ‘30.000000‘, ‘79.800000‘, ‘73.400000‘], # 鸡屁股下 74 [‘45.500000‘, ‘42.000000‘, ‘105.000000‘, ‘91.500000‘], #鸡屁股上补充 75 [‘30.000000‘, ‘21.000000‘, ‘105.000000‘, ‘97.300000‘], # 鸡下 76 [‘42.000000‘, ‘30.000000‘, ‘97.300000‘, ‘79.800000‘], #鸡下补充 77 [‘21.000000‘, ‘3.000000‘, ‘129.000000‘, ‘105.000000‘], # 鸡脚 (南海) 78 ] 79 # ——测试使用——开始———— 80 precision = Decimal(‘1.0001000‘) # 精度 0.0001000==1000米 81 # ——测试使用——结束———— 82 83 #——实际获取时替换——精度自己控制————建议是 0.0001000 ————开始———— 84 #precision = Decimal(‘0.0001000‘) # 精度 0.0001000==1000米 85 # ——实际获取时替换——精度自己控制————建议是 0.0001000 ————结束———— 86 87 file_name=0 88 89 thre_list=[] #线程列表 90 for single_list in LogLatList: 91 ctime=time.time()#当前时间 92 temp=Thre(single_list,precision,ctime,file_name)#实例化类 93 file_name+=1 94 thre_list.append(temp)#线程列表 95 for thre in thre_list:#线程列表 96 thre.start()#执行单个线程 97 98 if __name__ == ‘__main__‘: 99 main()
写入到文件
原文地址:https://www.cnblogs.com/ujq3/p/9775376.html
时间: 2024-10-25 13:08:49