关键字查询地理经纬度及省份-市

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号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值