uge3

因为要做一个前端画图需要经纬度,一个个的查询过麻烦,最终弄出这个,以备后查!

 

  1 import threading , time
  2 import requests
  3 from decimal import Decimal
  4 #爬取数据
  5 def hq(address,name_id):
  6     url = \'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=\' + str(address)
  7     response = requests.get(url)
  8     answer = response.json()
  9     print(\'得到反解数据\', answer)
 10     lng = answer[\'result\'][\'location\'][\'lng\']
 11     lat = answer[\'result\'][\'location\'][\'lat\']
 12     formatted_address = answer[\'result\'][\'formatted_address\']
 13     business = answer[\'result\'][\'business\']
 14     city = answer[\'result\'][\'addressComponent\'][\'city\']
 15     direction = answer[\'result\'][\'addressComponent\'][\'city\']
 16     distance = answer[\'result\'][\'addressComponent\'][\'direction\']
 17     district = answer[\'result\'][\'addressComponent\'][\'district\']
 18     province = answer[\'result\'][\'addressComponent\'][\'province\']
 19     street = answer[\'result\'][\'addressComponent\'][\'street\']
 20     street_number = answer[\'result\'][\'addressComponent\'][\'street_number\']
 21     cityCode = answer[\'result\'][\'cityCode\']
 22     lin_list = str(\'%6f\' % lng) + \'|\' + str(\'%6f\' % lat) + \'|\' + str(formatted_address) + \'|\' + str(business) + \'|\' + str(
 23         city) + \'|\' + str(direction) + \'|\' + str(distance) + \'|\' + str(district) + \'|\' + str(province) + \'|\' + str(
 24         street) + \'|\' + str(street_number) + \'|\' + str(cityCode)
 25     if cityCode==0:
 26         #print(\'外国\')
 27         pass
 28     else:
 29    
 30         name=str(name_id)+\'list\'
 31         print(name)
 32         with open(name, \'a+\', encoding=(\'utf-8\')) as f:
 33             f.write(lin_list+\'\n\')
 34         print(\'文件写入完成\')
 35 
 36 #经度纬度处理
 37 def longitude_proces(longitude, interval, latitude, latitude_end, name_id):
 38     while longitude >= latitude:
 39         address = \'%s,%s\' % (longitude, latitude_end)  # 请求时,经度,纬度,需要互换
 40         hq(address, name_id)
 41         longitude -= interval
 42 
 43 
 44 class Thre(threading.Thread):#继承线程中的类
 45     def __init__(self,lists,interval,name_id,times):
 46         super(Thre,self).__init__()
 47         self.interval=interval
 48         self.lists=lists
 49         self.name_id=name_id
 50         self.times=times
 51     def run(self):
 52         print(\'执行线程开始时间:\',self.times,self.lists,\'==================start=============================================\')
 53         slog,elog,slat,elat=self.lists
 54         #print(slog,elog,slat,elat)
 55         longitude = Decimal(slog)#经度longitude开始
 56         longitude_end = Decimal(elog)#经度结束
 57         latitude=Decimal(slat)# 纬度latitude开始
 58         latitude_end=Decimal(elat)#纬度结束
 59         
 60         while latitude>=latitude_end:
 61             latitude -= self.interval
 62             longitude_proces(longitude,self.interval,longitude_end,latitude,self.name_id)
 63         else:
 64             den_time=time.time()-self.times
 65             print(\'执行线程所用时间:\',den_time,self.lists,\'==================end=============================================\')
 66         
 67 
 68 
 69 def main():
 70     itude_list=[
 71         [\'42.000000\', \'30.000000\', \'105.000000\', \'79.800000\'],  # 
 72         [\'42.000000\', \'21.000000\', \'129.000000\', \'105.000000\'],  # 
 73         [\'50.000000\', \'42.000000\', \'135.000000\',\'129.000000\'],  #
 74         [\'54.000000\', \'42.000000\', \'129.000000\', \'115.000000\'],  #
 75         [\'45.500000\', \'42.000000\', \'115.000000\', \'105.000000\'],  #
 76         [\'49.200000\', \'42.000000\', \'91.500000\', \'79.800000\'],  # 
 77         [\'42.000000\', \'30.000000\', \'79.800000\', \'73.400000\'],  # 
 78         [\'45.500000\', \'42.000000\', \'105.000000\', \'91.500000\'],  #
 79         [\'30.000000\', \'21.000000\', \'105.000000\', \'97.300000\'],  # 
 80         [\'42.000000\', \'30.000000\', \'97.300000\', \'79.800000\'],  #
 81         [\'21.000000\', \'3.000000\', \'129.000000\', \'105.000000\'],  # (南海)
 82     ]
 83 
 84     interval = Decimal(\'3.0001000\')#间隔
 85     number = 0
 86     thre_list=[]
 87 
 88     for itude in itude_list:
 89         start_times=time.time()
 90         number += 1
 91         temp=Thre(itude_list,interval,number,start_times)
 92         thre_list.append(temp)
 93         
 94     for thre in thre_list:
 95         thre.start()
 96         
 97 
 98 
 99 if __name__ == \'__main__\':
100     main()

 

分类:

技术点:

相关文章: