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

时间:2022-10-14 21:46:29

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

爬虫——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爬虫

爬虫——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)
lng = answer['result']['location']['lng']
lat = answer['result']['location']['lat']
formatted_address = answer['result']['formatted_address']
business = answer['result']['business']
city = answer['result']['addressComponent']['city']
direction = answer['result']['addressComponent']['city']
distance = answer['result']['addressComponent']['direction']
district = answer['result']['addressComponent']['district']
province = answer['result']['addressComponent']['province']
street = answer['result']['addressComponent']['street']
street_number = answer['result']['addressComponent']['street_number']
cityCode = answer['result']['cityCode']
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)
print(s)

解析数据

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

http://api.map.baidu.com/lbsapi/getpoint/index.html

 #鸡头1
#129.000000,54.000000 #锚点(鸡头)
#115.000000,54.0000000
#115.000000,42.0000000 #北京
#129.000000,42.000000 #沈阳
#['54.0000000', '42.000000', '129.000000', '115.000000'],#鸡头1 #鸡头2
#129.000000,42.000000 #沈阳
#135.200000,42.000000
#129.000000,50.000000
#135.200000,50.000000
#['50.000000','42.000000','135.000000','129.000000'],#鸡头2
#鸡头补充
#115.000000,42.0000000 #北京
#105.0000000,42.000000 #锚点(银川上面)
#105.0000000,45.500000 #乌兰巴托
#115.000000,45.500000
#['45.500000', '42.000000', '115.000000', '105.000000'],#鸡头补充 #鸡屁股上补充
#105.0000000,42.000000 #锚点(银川上面)
#91.500000,45.500000 #乌鲁木齐
#105.0000000,45.500000 #乌兰巴托
#91.500000,42.000000 #锚点(鸡屁股)
#['45.500000', '42.000000', '105.0000000', '91.500000'],#鸡屁股上补充
#鸡屁股上
#91.500000,42.000000 #锚点(鸡屁股)
#79.800000,42.000000 #啊拉木图
#79.800000,49.200000
#91.500000,49.200000
#['49.200000', '42.000000', '91.500000', '79.800000'],#鸡屁股上
#鸡屁股下
#79.800000,42.000000 #啊拉木图
#79.800000,30.0000000 #巴雷利
#73.400000,42.000000
#73.400000,30.000000
#['42.000000', '30.000000', '79.800000', '73.400000'],#鸡屁股下
#鸡下补充
#79.800000,30.0000000 #巴雷利
#97.300000,30.000000 #拉萨昌都(锚点)
#97.300000,26.800000
#79.800000,26.800000
#['42.000000', '30.000000', '97.300000', '79.800000'],#鸡下补充
#鸡下
#97.300000,30.000000 #拉萨昌都(锚点)
#105.000000,30.000000 #(成都重庆贵阳)
#105.000000,21.000000 #河内
#97.300000,21.000000 #曼德勒
#['30.000000', '21.000000', '105.000000', '97.300000'],#鸡下 #鸡胸
#105.0000000,42.000000 #锚点(银川上面)
#105.0000000,21.000000 #河内
#129.000000,42.000000 #沈阳
#129.000000,21.000000 #台北(海)
#['42.000000', '21.000000', '129.000000', '105.000000'],#鸡胸 #鸡身
#105.000000,42.000000 #锚点(银川上面)
#79.800000,42.000000 #啊拉木图
#79.800000,30.000000 #巴雷利
#105.000000,30.000000 #(成都重庆贵阳)
#['42.000000', '30.000000', '105.000000', '79.800000'],#鸡身
#鸡脚 (南海)
#105.000000,21.000000 #河内
#129.000000,21.000000 #台北(海)
#105.0000000,3.000000 #新加坡
#129.000000,3.000000 #海(苏拉威西海)
#['21.000000', '3.000000', '129.000000', '105.000000'],

经纬度分析

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

 import requests
from decimal import Decimal
def obtain(address):
url = 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=' + str(address)
response = requests.get(url)
answer = response.json()
print('得到反解数据', answer)
lng = answer['result']['location']['lng']
lat = answer['result']['location']['lat']
formatted_address = answer['result']['formatted_address']
business = answer['result']['business']
city = answer['result']['addressComponent']['city']
direction = answer['result']['addressComponent']['city']
distance = answer['result']['addressComponent']['direction']
district = answer['result']['addressComponent']['district']
province = answer['result']['addressComponent']['province']
street = answer['result']['addressComponent']['street']
street_number = answer['result']['addressComponent']['street_number']
cityCode = answer['result']['cityCode']
s = str('%6f' % lng) + '|' + str('%6f' % lat) + '|' + str(formatted_address) + '|' + str(business) + '|' + str(
city) + '|' + str(direction) + '|' + str(distance) + '|' + str(district) + '|' + str(province) + '|' + str(
street) + '|' + str(street_number) + '|' + str(cityCode)
print(s) def longitude(slon,precision,elon,slat):
while slon >= elon:
address = '%s,%s' % (slon,slat) # 请求用的,经度,纬度,
obtain(address)#传入经度,纬度,获取地址地名
slon -= precision #['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
slon = Decimal('42.000000')#经度longitude开始
elon = Decimal('30.000000')#经度结束
slat=Decimal('105.000000')# 纬度latitude开始
elat=Decimal('79.800000')#纬度结束
precision = Decimal('5.0001000')#精度 0.0001000==1000米
while slat>=elat:
slat -= precision
longitude(slon,precision,elon,slat)

根据精度获取多个地名

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

 import requests
from decimal import Decimal
def obtain(address):
url = 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=' + str(address)
response = requests.get(url)
answer = response.json()
print('得到反解数据', answer)
lng = answer['result']['location']['lng']
lat = answer['result']['location']['lat']
formatted_address = answer['result']['formatted_address']
business = answer['result']['business']
city = answer['result']['addressComponent']['city']
direction = answer['result']['addressComponent']['city']
distance = answer['result']['addressComponent']['direction']
district = answer['result']['addressComponent']['district']
province = answer['result']['addressComponent']['province']
street = answer['result']['addressComponent']['street']
street_number = answer['result']['addressComponent']['street_number']
cityCode = answer['result']['cityCode']
s = str('%6f' % lng) + '|' + str('%6f' % lat) + '|' + str(formatted_address) + '|' + str(business) + '|' + str(
city) + '|' + str(direction) + '|' + str(distance) + '|' + str(district) + '|' + str(province) + '|' + str(
street) + '|' + str(street_number) + '|' + str(cityCode)
print(s) def longitude(slon,precision,elon,slat):
while slon >= elon:
address = '%s,%s' % (slon,slat) # 请求用的,经度,纬度,
obtain(address)#传入经度,纬度,获取地址地名
slon -= precision LogLatList=[
['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
['42.000000', '21.000000', '129.000000', '105.000000'], # 鸡胸
['50.000000', '42.000000', '135.000000','129.000000'], #鸡头2
['54.000000', '42.000000', '129.000000', '115.000000'], #鸡头1
['45.500000', '42.000000', '115.000000', '105.000000'], #鸡头补充
['49.200000', '42.000000', '91.500000', '79.800000'], # 鸡屁股上
['42.000000', '30.000000', '79.800000', '73.400000'], # 鸡屁股下
['45.500000', '42.000000', '105.000000', '91.500000'], #鸡屁股上补充
['30.000000', '21.000000', '105.000000', '97.300000'], # 鸡下
['42.000000', '30.000000', '97.300000', '79.800000'], #鸡下补充
['21.000000', '3.000000', '129.000000', '105.000000'], # 鸡脚 (南海)
]
precision = Decimal('10.0001000') # 精度 0.0001000==1000米
for single_list in LogLatList:
slons,elons,slats,elats=single_list
#['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
slon = Decimal(slons)#经度longitude开始
elon = Decimal(elons)#经度结束
slat=Decimal(slats)# 纬度latitude开始
elat=Decimal(elats)#纬度结束 while slat>=elat:
slat -= precision
longitude(slon,precision,elon,slat)

批量获取数据

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

 import requests
from decimal import Decimal
import threading , time
def obtain(address):
url = 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=' + str(address)
response = requests.get(url)
answer = response.json()
print('得到反解数据', answer)
lng = answer['result']['location']['lng']
lat = answer['result']['location']['lat']
formatted_address = answer['result']['formatted_address']
business = answer['result']['business']
city = answer['result']['addressComponent']['city']
direction = answer['result']['addressComponent']['city']
distance = answer['result']['addressComponent']['direction']
district = answer['result']['addressComponent']['district']
province = answer['result']['addressComponent']['province']
street = answer['result']['addressComponent']['street']
street_number = answer['result']['addressComponent']['street_number']
cityCode = answer['result']['cityCode']
s = str('%6f' % lng) + '|' + str('%6f' % lat) + '|' + str(formatted_address) + '|' + str(business) + '|' + str(
city) + '|' + str(direction) + '|' + str(distance) + '|' + str(district) + '|' + str(province) + '|' + str(
street) + '|' + str(street_number) + '|' + str(cityCode)
print(s) def longitude(slon,precision,elon,slat):
while slon >= elon:
address = '%s,%s' % (slon,slat) # 请求用的,经度,纬度,
obtain(address)#传入经度,纬度,获取地址地名
slon -= precision class Thre(threading.Thread):#继承线程中的类
#Thre(single_list,precision,ctime)
def __init__(self,single_list,precision,ctime):
super(Thre,self).__init__()#重新写父类,解决多继承问题
self.precision=precision#精度
self.single_list=single_list#单列表
self.ctime=ctime#当前时间
def run(self):
print('执行线程开始时间:', self.ctime, '执行的列表:',self.single_list)
slons, elons, slats, elats = self.single_list
# ['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
slon = Decimal(slons) # 经度longitude开始
elon = Decimal(elons) # 经度结束
slat = Decimal(slats) # 纬度latitude开始
elat = Decimal(elats) # 纬度结束
while slat >= elat:
slat -= self.precision
longitude(slon,self.precision, elon, slat)
else:
end_time=time.time()-self.ctime
print('执行线程所用时间:',end_time, '执行的列表:',self.single_list) def main():
LogLatList=[
['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
['42.000000', '21.000000', '129.000000', '105.000000'], # 鸡胸
['50.000000', '42.000000', '135.000000','129.000000'], #鸡头2
['54.000000', '42.000000', '129.000000', '115.000000'], #鸡头1
['45.500000', '42.000000', '115.000000', '105.000000'], #鸡头补充
['49.200000', '42.000000', '91.500000', '79.800000'], # 鸡屁股上
['42.000000', '30.000000', '79.800000', '73.400000'], # 鸡屁股下
['45.500000', '42.000000', '105.000000', '91.500000'], #鸡屁股上补充
['30.000000', '21.000000', '105.000000', '97.300000'], # 鸡下
['42.000000', '30.000000', '97.300000', '79.800000'], #鸡下补充
['21.000000', '3.000000', '129.000000', '105.000000'], # 鸡脚 (南海)
]
precision = Decimal('20.0001000') # 精度 0.0001000==1000米 thre_list=[] #线程列表
for single_list in LogLatList:
ctime=time.time()#当前时间
temp=Thre(single_list,precision,ctime)#实例化类
thre_list.append(temp)#线程列表
for thre in thre_list:#线程列表
thre.start()#执行单个线程 if __name__ == '__main__':
main()

多线程获取数据

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

 import requests
from decimal import Decimal
import threading , time
def obtain(address,file_name):
url = 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=' + str(address)
response = requests.get(url)
answer = response.json()
print('得到反解数据', answer)
lng = answer['result']['location']['lng']
lat = answer['result']['location']['lat']
formatted_address = answer['result']['formatted_address']
business = answer['result']['business']
city = answer['result']['addressComponent']['city']
direction = answer['result']['addressComponent']['city']
distance = answer['result']['addressComponent']['direction']
district = answer['result']['addressComponent']['district']
province = answer['result']['addressComponent']['province']
street = answer['result']['addressComponent']['street']
street_number = answer['result']['addressComponent']['street_number']
cityCode = answer['result']['cityCode']
s = str('%6f' % lng) + '|' + str('%6f' % lat) + '|' + str(formatted_address) + '|' + str(business) + '|' + str(
city) + '|' + str(direction) + '|' + str(distance) + '|' + str(district) + '|' + str(province) + '|' + str(
street) + '|' + str(street_number) + '|' + str(cityCode)
print(s)
if cityCode==0:
#print('外国')
pass
else:
#print(s)
fname=str(file_name)+'list'
with open(fname, 'a+', encoding=('utf-8')) as f:
f.write(s+'\n') def longitude(slon,precision,elon,slat,file_name):
while slon >= elon:
address = '%s,%s' % (slon,slat) # 请求用的,经度,纬度,
obtain(address,file_name)#传入经度,纬度,获取地址地名
slon -= precision class Thre(threading.Thread):#继承线程中的类
#Thre(single_list,precision,ctime)
#temp = Thre(single_list, precision, ctime, file_name) # 实例化类
def __init__(self,single_list,precision,ctime,file_name):
super(Thre,self).__init__()#重新写父类,解决多继承问题
self.precision=precision#精度
self.single_list=single_list#单列表
self.ctime=ctime#当前时间
self.file_name=file_name#文件名
def run(self):
print('执行线程开始时间:', self.ctime, '执行的列表:',self.single_list)
slons, elons, slats, elats = self.single_list
# ['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
slon = Decimal(slons) # 经度longitude开始
elon = Decimal(elons) # 经度结束
slat = Decimal(slats) # 纬度latitude开始
elat = Decimal(elats) # 纬度结束
while slat >= elat:
slat -= self.precision
longitude(slon,self.precision, elon, slat,self.file_name)
else:
end_time=time.time()-self.ctime
print('执行线程所用时间:',end_time, '执行的列表:',self.single_list) def main():
LogLatList=[
['42.000000', '30.000000', '105.000000', '79.800000'], # 鸡身
['42.000000', '21.000000', '129.000000', '105.000000'], # 鸡胸
['50.000000', '42.000000', '135.000000','129.000000'], #鸡头2
['54.000000', '42.000000', '129.000000', '115.000000'], #鸡头1
['45.500000', '42.000000', '115.000000', '105.000000'], #鸡头补充
['49.200000', '42.000000', '91.500000', '79.800000'], # 鸡屁股上
['42.000000', '30.000000', '79.800000', '73.400000'], # 鸡屁股下
['45.500000', '42.000000', '105.000000', '91.500000'], #鸡屁股上补充
['30.000000', '21.000000', '105.000000', '97.300000'], # 鸡下
['42.000000', '30.000000', '97.300000', '79.800000'], #鸡下补充
['21.000000', '3.000000', '129.000000', '105.000000'], # 鸡脚 (南海)
]
# ——测试使用——开始————
precision = Decimal('1.0001000') # 精度 0.0001000==1000米
# ——测试使用——结束———— #——实际获取时替换——精度自己控制————建议是 0.0001000 ————开始————
#precision = Decimal('0.0001000') # 精度 0.0001000==1000米
# ——实际获取时替换——精度自己控制————建议是 0.0001000 ————结束———— file_name=0 thre_list=[] #线程列表
for single_list in LogLatList:
ctime=time.time()#当前时间
temp=Thre(single_list,precision,ctime,file_name)#实例化类
file_name+=1
thre_list.append(temp)#线程列表
for thre in thre_list:#线程列表
thre.start()#执行单个线程 if __name__ == '__main__':
main()

写入到文件

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