地理编码(待重构)

# -*- coding: utf-8 -*-

import pypyodbc
import requests
from bs4 import BeautifulSoup

#获取access文件中的记录,filepath路径,tablename表名
def get_rows_from_mdb(filepath,tablename):
    conn = pypyodbc.connect(u'Driver={Microsoft Access Driver (*.mdb)};DBQ=' + filepath)
    cur = conn.cursor()
    try:
        cur.execute('select * from %s ' % tablename)
        records = cur.fetchall()
    except:
        print '读取文件失败'
    return records

#调用百度地图API接口,address为地名,ak为百度秘钥
def getlocation(address,ak):
    try:
        baseurl = 'http://api.map.baidu.com/geocoder/v2/?address='
        myak = '&ak=' + ak
        url = baseurl + address + myak
        response = requests.get(url)
        responseinfo = response.content
        soup = BeautifulSoup(responseinfo,'lxml')
        lng = soup.lng.string
        lat = soup.lat.string
        location = lng + ',' + lat
    except:
        location = 'UNKWON'
    return location

yy = get_rows_from_mdb('E:/qqq.mdb','b1')
output = open('data.txt', 'w+')
fields = '字段1'+','+'字段2'+','+'字段3'+','+'字段4'
output.write(fields+'\n')
for y in yy:
    print y[0],y[1],y[2]
    rowline = str(y[0]) + ',' + y[1] + ',' +y[2]
    output.write(rowline+'\n')
output.close()
#xx = getlocation('babb路','dte7eUyXPMwEqhn6xb66HbxVaBrNpkab')

你可能感兴趣的:(地理编码(待重构))