import requests
import time
import json
import pandas as pd
import time
#可以这在这个网站上校验数据 http://www.gpsspg.com/maps.htm
#若Cookie或是相关headers中的参数失效,请到上面的网站上按F12重新获取。
def get_detail_info(keyword):
headers = {
'Accept':'*/*',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection':'keep-alive',
'Cookie':'_ga=GA1.2.917569749.1577680738; RK=bULhj4O5se; ptcz=25678d17ed1fc498f1b9208463e0cf97e47deeab44ddc55c2fe9b627d99472db; pgv_info=ssid=s1021528919; pgv_pvid=6891720202; mpuv=201c1f78-04a7-40f9-a668-71ba30e44c4f',
'Host':'apis.map.qq.com',
'Referer':'http://www.gpsspg.com/iframe/maps/qq_191222.htm?mapi=2&t='+str(time.time())[0:14].replace('.',''),
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}
addr = str(keyword)
url = 'https://apis.map.qq.com/jsapi?qt=geoc&addr=' + addr + '&key=FBOBZ-VODWU-C7SVF-B2BDI-UK3JE-YBFUS&output=jsonp&pf=jsapi&ref=jsapi&cb=qq.maps._svcb3.geocoder0'
html = requests.get(url = url ,headers=headers)
html = html.text.replace('qq.maps._svcb3.geocoder0&&qq.maps._svcb3.geocoder0(','')[0:-1]
# print(html)
try:
json_data = json.loads(html)
detail = json_data['detail']
# print(detail)
# print(detail['errmsg'])
try:
if detail['errmsg'] == 'no result':
return {'addr':addr,'province':'','city':'','district':'','latitude':'','longitude':''}
else:
# print(1)
pointx = detail['pointx']#经度
pointy = detail['pointy']#纬度
province = detail['province']
city = detail['city']
district = detail['district']
# print(pointy,pointx,province,city,district)
return {'addr':addr,'province':province,'city':city,'district':district,'latitude':pointy,'longitude':pointx}
except Exception as e:
# print(e)
pointx = detail['pointx']#经度
pointy = detail['pointy']#纬度
province = detail['province']
city = detail['city']
district = detail['district']
# print(pointy,pointx,province,city,district)
return {'addr':addr,'province':province,'city':city,'district':district,'latitude':pointy,'longitude':pointx}
except Exception as e:
return {'addr':'','province':'','city':'','district':'','latitude':'','longitude':''}
keyword = '上海市人民广场地铁站'
dt = get_detail_info(keyword)
print(dt)
结果如下:
注意:
1、关键字并不能定位所有的经纬度。
2、地址尽量是省+市区+区/县+街道/路X号。如:江苏省苏州市姑苏区三香路18号