zoukankan      html  css  js  c++  java
  • 第一部分:地域维度标准化

    正在更新数据库,但是今日限额到了,api接口不能用了,数据表只更新了一半。

    期间遇到了很多问题,经过问同学,和查资料解决了。

    上一篇博客的获取单个地址比较好用,批量获取的时候就不太行了。

    这是地域与行政代码的截图:

     主要代码:

    获取经纬度:

    def JingWei(address):
        global lat, lng
        # 产生sn码
        queryStr = "/geocoding/v3/?address=" + address + '&output=json&ak=' + MyAK
        encodedStr = urllib.parse.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
        rawStr = encodedStr + MySK
        sn = (hashlib.md5(urllib.parse.quote_plus(rawStr).encode("utf8")).hexdigest())
    
        # 生成url
        url = urllib.parse.quote("http://api.map.baidu.com" + queryStr + "&sn=" + sn, safe="/:=&?#+!$,;'@()*[]")
        #print('Retrieving', url)
    
        # 从API读取数据
        uh = urllib.request.urlopen(url)
        data = uh.read().decode()
        # print('Retrieved', len(data), 'characters')
        # 解析数据
        try:
            js = json.loads(data)
        except:
            js = None
        try:
            if not js or 'status' not in js or js['status'] != 0:
                print('======Failure====')
                print(data)
    
        # print(json.dumps(js, indent=4, ensure_ascii=False))
    
            # 获取经纬度坐标和地址类型
            lat = js["result"]["location"]["lat"]
            lng = js["result"]["location"]["lng"]
            #print('纬度', lat, '经度', lng)
            level = js["result"]["level"]
            #print('地址类型', level)
        except:
            pass
    View Code

    逆向解析:

    def getlocation(lat, lng):
        url = '/reverse_geocoding/v3/?ak=' + MyAK + '&output=json&coordtype=wgs84ll&location=' + str(lat) + ',' + str(lng)
        encodedStr = urllib.parse.quote(url, safe="/:=&?#+!$,;'@()*[]")
        rawStr = encodedStr + MySK
        sn = (hashlib.md5(urllib.parse.quote_plus(rawStr).encode("utf8")).hexdigest())
        # 生成url
        url = urllib.parse.quote("http://api.map.baidu.com" + url + "&sn=" + sn, safe="/:=&?#+!$,;'@()*[ ]")
        # print('Retrieving', url)
        req = urllib.request.urlopen(url)  # json格式的返回数据
        res = req.read().decode("utf-8")  # 将其他编码的字符串解码成unicode
        #print(json.loads(res))
        return json.loads(res)
    View Code
    def jsonFormat(addr,lat, lng):
        print(addr)
        str = getlocation(lat, lng)
        global dictjson
        dictjson = {}
        # get()获取json里面的数据
        jsonResult = str.get('result')
        address = jsonResult.get('addressComponent')
        # 国家
        country = address.get('country')
        # 国家编号(0:中国)
        country_code = address.get('country_code')
        #
        province = address.get('province')
        # 城市
        city = address.get('city')
        # 城市等级
        city_level = address.get('city_level')
        # 县级
        district = address.get('district')
        # 区划代码
        adcode = address.get('adcode')
        # 街道
        street = address.get('street')
        # 街道编号
        street_number = address.get('street_number')
        # 把获取到的值,添加到字典里(添加)
        # dictjson['country']=country
        # dictjson['country_code'] = country_code
       # dictjson['province'] = province + city + district + street + street_number
        #dictjson['province'] = province + city + district
        #dictjson['adcode'] = adcode
        province=province + city + district
        db.update_db(addr,'地域',province)
        db.update_db(addr, '行政代码', adcode)
       # with open('D:\workspace\pycharm\region\adcode.json', 'a',encoding='utf-8') as f:
            #json.dump(dictjson, f,ensure_ascii=False)
        try:
            conn = connect(host='localhost', port=3306, database='kettle',
                       user='root',
                       password='1234', charset='utf8')
            cs = conn.cursor()
            sql = "update exam12 set 地域='"+province+"' where 完成单位='"+addr+"'"
            cs.execute(sql)
        except:
            pass
    
        #return dictjson
        # dictjson['city_level'] = city_level
        # dictjson['district']=district
    View Code
  • 相关阅读:
    webpack打包踩坑记录
    node笔记
    你真的会Xilinx FPGA的复位吗?
    Verilog 99题之001-009
    数字电路基础
    跨时钟域处理
    时序逻辑电路基础
    FPGA&ASIC基本开发流程
    关于FPGA的一些小见解
    基于FPGA的I2C读写EEPROM
  • 原文地址:https://www.cnblogs.com/zmh-980509/p/12482954.html
Copyright © 2011-2022 走看看