dealAddressConvert.py 3.63 KB
import logging
import pandas as pdas
import os
import chardet
import datetime
import requests

import openpyxl
from openpyxl import Workbook

from djcardrenewal.common.dbase.MysqlHelper import MysqlHelper
from djcardrenewal.common.dbase import DbConect
from djcardrenewal.common.sqlcontent import DjSql
from djcardrenewal.common.sqlcontent import CurMonthYzl
from djcardrenewal.common.string.StringBuilder import StringBuilder

print("sys config " + __name__)
log_name = './log/addressConvert/dealAddressConvert.log'
log_format = '[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]'
log_level = logging.DEBUG
log_mode = 'a'
logging.basicConfig(filename=log_name, format=log_format, level=log_level, filemode=log_mode)

class DealAddressConvertMain:

    handleDescription = ''
    handleMonth = ''
    hmonth = ''
    module_file_path = ''
    defaultExportReleasePath = ''

    db = None

    def __init__(self, handle_month):
        logging.info("init construct bean")
        self.handleDescription = "跑地址通过高德转..."
        self.handleMonth = handle_month
        self.hmonth = handle_month.replace('-', '')[2:6]

    def initCurrentMonthTable(self):
        logging.info("初始化:暂无操作")
        
    # 判断文件编码
    def detect_file_encoding(self, filename):
        with open(filename, 'rb') as f:
            content = f.read()
            result = chardet.detect(content)
            return result['encoding']
    
    def dealDataToExcel(self, sourceFile):
        print('deal start...')
        # 读取 Excel 文件
        dataframes_source = pdas.read_excel(sourceFile)
        dataframes_source.columns = dataframes_source.columns.str.strip()

        for index,drow in dataframes_source.iterrows():
            url_gaod_server = f'https://restapi.amap.com/v3/place/text?key=373bd88fa36ebc0247d60281386b6cec&keywords={drow.门店名称}&types=&city=&children=&offset=&page=1&extensions=base'
            print(url_gaod_server)

            merchantAddress = ''
            carType = ''
            telNum = ''

            # 发起GET请求
            response = requests.get(url_gaod_server)
            if response.status_code == 200:
                # 解析JSON响应
                data = response.json()
                print(data)
                # 检查是否有POI数据
                if 'pois' in data:
                    poiList = data['pois']
                    if len(poiList)>0:
                        poiItem = poiList[0]
                        merchantAddress = f'{poiItem["pname"]}{poiItem["cityname"]}{poiItem["adname"]}{poiItem["address"]}'
                        telNum = poiItem["tel"]
                        carType = poiItem["type"]
                        print(merchantAddress, telNum, carType)
            
            dataframes_source.at[index, '地址'] = merchantAddress
            dataframes_source.at[index, '品牌'] = carType
            dataframes_source.at[index, '门店联系方式'] = telNum
        
        print('deal success')
        dataframes_source.to_excel('D:/users/address/门店清单-230920_convert_result.xlsx', index=False)
        # dataframes_source.to_excel('D:/users/address/test-230920_convert_result.xlsx', index=False)



if __name__ == "__main__":
    handle_month = '2023'
    print('==========================跑地址通过高德转==========================')

    
    #启动初始化
    dealMain = DealAddressConvertMain(handle_month)
    logging.info("start handle" + dealMain.handleDescription + ' the month:' + dealMain.handleMonth)

    sourceFile = 'D:/users/address/门店清单-230920.xlsx'
    # sourceFile = 'D:/users/address/test.xlsx'
    dealMain.dealDataToExcel(sourceFile)