经纬度地址转换的方法集合(Python描述)

Python 2.7
IDE Pycharm 5.0.3
Geopy 1.11


前言

这只是我想做的一部分,写一块太大了,单独记录

目的

获取2015年GDP TOP100城市并获取城市对应经纬度,存入txt后续操作

获取GDP TOP100城市

查询网址如下http://www.redsh.com/a/20160126/171501.shtml
2015中国100大城市GDP排行榜TOP100出炉!

页面的效果大概是这样的~惨不忍睹的格式,花了我好多时间提取。。。。

元素大概是这样的

ok,先把TOP100城市爬下来再说


方法1:静态爬取

采用urllib2+BS4+re(更多静态爬取方法点击这里),当然你也可以采用request来代替urllib2,这里我只做个试验,所以用了urllib2,废话不多说,直接上代码!

# 采用urllib2+BS4进行静态网页解析
def getCityByBS4(url):

    html = urllib2.urlopen(urllib2.Request(url)).read().decode('gbk')
    soup = BeautifulSoup(html,'lxml')
    x = soup.find_all('p')
    t=1
    cityLocationList = []
    cityLocationListOnlyLatLng =[]
    for i in x:
        if t > 5 and t<106:
            city1 = i.get_text()
            city1 = city1.encode('utf-8')
            # 数据清洗过程
            city1 = re.split('\.',city1)[1]
            city1 = re.split('(',city1)[0]
            if len(city1)>13:
                city2 = city1[0:6]
            else:
                city2 =city1.strip()
            try:
                lat_lng = getLocation_xml(city2+"市")
                city_location = "%s %s"%(city2,str(lat_lng))
                city_locationOnlyLatLng = lat_lng
                cityLocationList.append(city_location)
                cityLocationListOnlyLatLng.append(city_locationOnlyLatLng)
                #write2txt(city_location,"City&LocationBS4.txt")
                print city_location
            except:
                print "something wrong about city:",city2
        t +=1

    return cityLocationList,cityLocationListOnlyLatLng
# 返回了一个有城市和经纬度对应的列表如:上海 31.249162,121.487899
# 还返回了一个只有经纬度的列表如 :31.249162,121.487899



#写入txt操作子函数
def write2txt(file,txtname):

        f = open(txtname,'a')
        f.write(file)
        f.write("\n")
        f.close()
        # please use this with try except/finall f.close()


# 程序入口
if __name__ == '__main__':

    url = 'http://www.redsh.com/a/20160126/171501.shtml'
    city_locationList,cityLocationListOnlyLatLng= getCityByBS4(url)
    print city_locationList
    for i in city_locationList:
        write2txt(i,"city&locaionByBS4.txt")
    for j in cityLocationListOnlyLatLng:
        write2txt(j,"LocaionOnlyByBS4.txt")

最后效果如图所示,至于以后要用哪个直接选择就行了

有地址对应和只有经纬度的


方法2:动态爬取

采用Selenium+PhantomJS的方法(更多Selenium使用案例点击这里
(这里是有趣的专题),这回没用Firefox浏览器了,也就是演示作用而已,无头PhantomJS就行了,也是直接上代码

#采用selenium+phantomjs进行动态解析
def getCityBySelenium(url):
    driver = webdriver.PhantomJS(executable_path="phantomjs.exe")
    driver.get(url)
    cityLocationList = []
    cityLocationListOnlyLatLng =[]
    for i in range(6,106):
        elem_city = driver.find_element_by_xpath("//font[@id='zoom']/p[%d]"%i).text
        elem_city = elem_city.encode('utf-8')
        try:
            city = re.split("[\.]",elem_city)[1]
            city = re.split("(",city)
            city1 = city[0]
            # 一个中文字符占3个长度!!
            if len(city1)>13:
                city2 = city1[0:6]
            else:
                city2 =city1.strip()

            lat_lng = getLocation_xml(city2+"市")
            city_location = "%s %s"%(city2,str(lat_lng))
            city_locationOnlyLatLng = lat_lng
            #write2txt(city_location,"City&LocationBySelenium.txt")
            print city_location

            cityLocationList.append(city_location)
            cityLocationListOnlyLatLng.append(city_locationOnlyLatLng)
        except:
            print 'something wrong with ',elem_city
    # 调用完后记得关闭!!!!!不然phantomjs一直占用内存
    driver.close()
    driver.quit()
    return cityLocationList,cityLocationListOnlyLatLng

# 返回了一个有城市和经纬度对应的列表如:上海 31.249162,121.487899
# 还返回了一个只有经纬度的列表如 :31.249162,121.487899

#至于主程序,只要把city_locationList,cityLocationListOnlyLatLng= getCityByBS4(url)中的getCityByBS4(url)换成getCityBySelenium(url),之后改一下txt文件名就行了,当然,这些函数都是在一个py文件里的

效果就不展示了,和上面静态处理一样


静态处理 VS 动态处理

静态处理urllib/request
优点:速度快
缺点:处理动态网页抓取不全

动态处理Selenium+PhantomJS/Firefox
优点:能够处理任何静态、动态网页
缺点:速度慢

总结:如果能判断出来是静态页面的,使用urllib和request比较方便和快捷一点,但是设计交互元素比较多的动态网页,静态方法是没有办法处理好的,直接上Selenium把!


正题时间

获取城市之后,怎么转化为对应的经纬度呢,这里也有两个方法

方法1:调用API接口

简单一句话介绍API接口,你不需要知道里面是怎么操作的,只需要知道你输入什么,经过一些乱七八糟的处理后(不用你管),返回给你一个友好的值就可以了。

举个例子,我输入城市名字,调用接口后,它返回给我经纬度,ok,就是这么简单。

这次使用的是百度地图的API,详见官网百度地图API

就像访问网页抓取元素一样,然后她会返回json/xml格式的数据,只是,这回的url变成了如下

#获取json格式的url
http://api.map.baidu.com/geocoder?address=城市&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2
#返回xml格式的url
http://api.map.baidu.com/geocoder?address=城市&output=xml&key=f247cdb592eb43ebac6ccd27f796e2d2

以json返回的举个例子,xml一样道理

#调用API返回json格式

import urllib2
url= 'http://api.map.baidu.com/geocoder?address=北京&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2'
html = urllib2.urlopen(urllib2.Request(url))
json1 = html.read() #转化为str类型
print json1

返回的形式如下,只需要在爬取其中的lng和lat就行了,至于怎么取出其中的值,可以转化为json字典用字典的方法取值

    "status":"OK",
    "result":{
        "location":{
            "lng":116.395645,
            "lat":39.929986
        },
        "precise":0,
        "confidence":10,
        "level":"\u57ce\u5e02"
    }
}

完整的json格式调用子函数

def getLocation_json(addr):
    url= 'http://api.map.baidu.com/geocoder?address=%s&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2'%(addr)
    html = urllib2.urlopen(urllib2.Request(url))
    json1 = html.read() #转化为str类型

    hjson =json.loads(json1) #转化为dict类型
    lng = hjson['result']['location']['lng'] # 经度
    lat = hjson['result']['location']['lat'] # 纬度
    lng_lat = [lng, lat]
    return lng_lat
#返回json数据格式
{
    "status":"OK",
    "result":{
        "location":{
            "lng":116.395645,
            "lat":39.929986
        },
        "precise":0,
        "confidence":10,
        "level":"\u57ce\u5e02"
    }
}

而采用xml返回形式的如下所示,自己选择喜欢的返回形式利于你的下一步处理就可以了。

#返回xml数据格式
<?xml version="1.0" encoding="utf-8" ?> 
<GeocoderSearchResponse> 
    <status>OK</status>
    <result>
                    <location>
                <lat>39.929986</lat>
                <lng>116.395645</lng>
            </location> 
            <precise>0</precise>
            <confidence>10</confidence>
            <level>城市</level>
            </result>   
</GeocoderSearchResponse>

返回xml格式的完整子程序

def getLocation_xml(addr):
    url= 'http://api.map.baidu.com/geocoder?address=%s&output=xml&key=f247cdb592eb43ebac6ccd27f796e2d2'%(addr)
    html = urllib2.urlopen(urllib2.Request(url))
    xml = html.read()

    bs_getDetail = BeautifulSoup(xml,'lxml')
    #方法一,直接根据路径找
    lng =float(bs_getDetail.result.location.lng.string)
    lat = float(bs_getDetail.result.location.lat.string)

    '''
    #方法二,使用find方法+正则表达式
    lat = bs_getDetail.find('lat')
    lng = bs_getDetail.find('lng')
    pattern = '\d+\.\d+'
    lat = re.findall(pattern,str(lat))[0]
    lng = re.findall(pattern,str(lng))[0]
    '''
    lat_lng = "%s,%s"%(lat,lng)
    return lat_lng


方法2:使用Geopy包

贴心的开发者已经把它放在github上了,(点击这里进行下载)直接下载安装即可使用。很方便的!

官方用法实例

官方描述用法

实现的功能和上面调用API一样,输入城市输出经纬度,而且,还有输入经纬度反向输出城市的方法,很好用!

以下是我自己用的两个子函数

# 调用Geopy包进行处理-获取城市名
def getCitynameByGeo(lat_lng):
    #知道经纬度获取地址
    geolocator = Nominatim()
    location = geolocator.reverse(lat_lng)
    addr = location.address
    print addr
    cityname = re.split("[/,]",addr)[-5].strip()
    print cityname

    return addr,cityname

# 调用Geopy包进行处理-获取经纬度
def getLocationByGeo(cityname):
    #知道地址获取经纬度
    geolocator = Nominatim()
    location2 = geolocator.geocode(cityname)
    lat = location2.latitude
    lng = location2.longitude
    return "%s  %s,%s"%(cityname,lat,lng)

直接要用的时候调用就可以了,大家可以自己试试,这个就不用在自己对json或者xml数据进行清洗和裁剪了。省时省力。

测试一下
就拿刚才用第一种方法写进去的txt中数据进行测试

import re
#导入包,实例化对象
from geopy.geocoders import Nominatim

# 调用Geopy包进行处理-获取城市名
def getCitynameByGeo(lat_lng):
    #知道经纬度获取地址
    geolocator = Nominatim()
    location = geolocator.reverse(lat_lng)
    addr = location.address
    print addr
    cityname = re.split("[/,]",addr)[-5].strip()
    return addr,cityname

f = open("LocaionOnlyByBS4.txt")
lines = f.readlines()
for line in lines:
    getCitynameByGeo(line)

测试效果如下,还是很不错的,就是要提取城市的时候有点麻烦,格式并不是统一的。

长治路, 虹口区, 虹口区 (Hongkou), 上海市, 200080, 中国
织染局胡同, 东城区, 北京市, 东城区, 北京市, 100010, 中国
春风路, 东山街道, 越秀区 (Yuexiu), 广州市 / Guangzhou, 广东省, 港方口岸區 Hong Kong Port Area, 510623, 中国

...

调用API VS Geopy

Geopy比较好吧,毕竟很加符合pythonic,包之间的相互使用,界限分明,而且对于不熟悉bs4,json,re的童鞋来说,这个简直遇到了亲人一样亲切!

当然,API的好处也很多,至少我是第一次调用地图api,其中包含的内容太多了,可以返回的内容也不止经纬度那么简单,如果以后要返回其他的值,估计还是得靠api


9.18补充

之后的测试发现,geopy的能力仅限于大城市,如果街道的话,是会报错的,而调用API则照样能搜索出来,以下是测试图

调用百度地图API

import urllib2
url= 'http://api.map.baidu.com/geocoder?address=哈尔滨市中央大街112号&output=json&key=f247cdb592eb43ebac6ccd27f796e2d2'
html = urllib2.urlopen(urllib2.Request(url))
json1 = html.read() 
print json1

结果返回正常(当然精度不能保证,理由大家都懂的)

{
    "status":"OK",
    "result":{
        "location":{
            "lng":126.657717,
            "lat":45.773225
        },
        "precise":0,
        "confidence":10,
        "level":"\u57ce\u5e02"
    }
}

而采用geopy包实现操作

from geopy.geocoders import Nominatim
def getLocationByGeo(cityname):
    #知道地址获取经纬度
    geolocator = Nominatim()
    location2 = geolocator.geocode(cityname)
    lat = location2.latitude
    lng = location2.longitude
    return "%s  %s,%s"%(cityname,lat,lng)

x = getLocationByGeo("哈尔滨市中央大街112号")
print x

而返回报错了

AttributeError: 'NoneType' object has no attribute 'latitude'

所以,街道或者比较小的地方还是百度地图api比较靠谱

btw–大家都要联网才能用的0.0


Q&A

1.分割字符串的时候,竟然出现了�乱码问题,原来中文字符占三个长度,,,,好吧,用的挺长时间的len(),第一次遇到中文,额。

str1 = "中文"
print len(str1)
#6
#一个中文占了三个长度!!!!!,难怪出现长度分割�乱码问题!!!

2.个别城市调用API时出错,比如西安这个城市(使用geopy没有这个问题)

lng_lat = getLocation_json("西安")
print lng_lat

#输出以下
Traceback (most recent call last):
  File "C:/Users/MrLevo/PycharmProjects/test/llll.py", line 208, in <module>
    lng_lat = getLocation_json("西安")
  File "C:/Users/MrLevo/PycharmProjects/test/llll.py", line 203, in getLocation_json
    lng = hjson['result']['location']['lng'] # 经度
TypeError: list indices must be integers, not str

于是乎,抖了个机灵,加了个”市”,哈哈,解决!百度这API做的也不走心啊,这么大一个省会没有,测试之后,不仅仅是西安躺枪,100个城市,就有七八个读不出来,,,哎,我都加上了”市”,才行。

lng_lat = getLocation_json("西安市")
print lng_lat

#[108.953098, 34.2778]

总结

以后先找包,再找api,包能实现的,没必要那么复杂。
不过也训练了一下api的调用,熟悉了一下json,xml,re等。果然时间久了不用还是会忘的很快哈~

最后祝大家中秋快乐!然而我还在实验室加班。。。。苦并快乐着,哈哈


致谢

@MrLevo520–BeautifulSoup使用一两则(不定期补充)
@MrLevo520–用python做些有趣的事
@转–使用Python解析JSON数据的基本方法
@糖拌咸鱼–Json概述以及python对json的相关操作
@jqnatividad–github geopy
@百度地图API

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值