爬虫——python——百度地图经纬度查询——经纬度查看地点地名——利用百度API获取地名经纬度——爬取所有的中国地址

Posted 颜言

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫——python——百度地图经纬度查询——经纬度查看地点地名——利用百度API获取地名经纬度——爬取所有的中国地址相关的知识,希望对你有一定的参考价值。

  

 

 

 

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)
<

以上是关于爬虫——python——百度地图经纬度查询——经纬度查看地点地名——利用百度API获取地名经纬度——爬取所有的中国地址的主要内容,如果未能解决你的问题,请参考以下文章

百度地图--api--获取经纬度

如何在百度地图输入经纬度进行查询

如何用Python批量获取经纬度坐标

如何查询经纬度坐标

百度地图怎么输入经纬度查看地点

在百度地图里面怎么获取线路的经纬度