【Python】 调用百度地图API抓取西安市小区信息

前面有同学参加市场调查大赛,需要西安市的小区信息数据,一个小爬虫程序完美解决。


百度地图开放平台

详情访问:百度PlaceAPI
这里用到了矩形区域检索,具体参数说明如下:




调取详情

所需库:
import   MySQLdb, time, urllib2, json

定义属性: 定义属性:
    def __init__(self, location, city, into_db):
        self.location = location
        self.city = city
        self.ziduan = ['name', 'address', 'city', 'district', 'scope', 'crawler_time', 'location']
        self.seq = ['"', '"', '"', '"', '"', '', '"']
        self.into_db = into_db

抓取过程:

    def spider(self):

        dif = [self.location[0] - self.location[2], self.location[3] - self.location[1]]  # 经纬度范围
        b = [x / 100.0 + self.location[2] for x in xrange(int(dif[0] * 50))]
        c = [x / 100.0 + self.location[1] for x in xrange(int(dif[1] * 50))]

        d = [[x, y] for x in b for y in c]                                                # 构建小矩形框

        cnxn = MySQLdb.connect(host=self.into_db[0], user=self.into_db[1],
                               passwd=self.into_db[2], charset=self.into_db[3])           # 连接到MySQL

        cursor = cnxn.cursor()                                                            # 定义一个MySQL游标
        sql = "select name from house.community_info where  city = '{}' ".format(self.city)
        cursor.execute(sql)                                                               # 执行SQL语句

        url_database = [item[0] for item in cursor.fetchall()]         # 取出当前城市已有小区的名字

        cnxn.commit()                                                  # 提交事务!不然python无法执行sql操作
        dict_data_list = []                                            # 字典列表
        i = 0

        for x in d:                                                    # 遍历当前城市所有划分出来的小矩形

            html = urllib2.urlopen(
                r'http://api.map.baidu.com/place/v2/search?query=小区&bounds={},{},{},{}4&page_size=20&output=json&ak=byEXq8wAVGXWKqOI4sA99ErDAtqlP6Fk'
                    .format(x[0], x[1], x[0] + 0.01, x[1] + 0.01))
            b = html.read()

            c = json.loads(b)    # json
            #print 'josn->>', c, '<<--'

            if not c['results']:
                continue
            for x in c['results']:
                dict_data = {}
                dict_data['city'] = self.city
                dict_data['name'] = x['name'].encode('utf-8', 'ignore')
                dict_data['address'] = x['address'].encode('utf-8', 'ignore')

                try:
                    lng_lat = str(x['location']['lng']) + ',' + str(x['location']['lat'])
                except KeyError:
                    lng_lat = '0.0,0.0'

                dict_data['location'] = lng_lat
                lng_lat = ','.join(lng_lat.split(',')[::-1])
                html = urllib2.urlopen(
                    r'http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location={}&output=json&pois=1&ak=你的AK码'.format(
                        lng_lat))
                b = html.read()
                b = b.split('renderReverse&&renderReverse(')[1][:-1]
                c = json.loads(b)  # json
                dict_data['scope'] = c['result']['business'].split(',')[0].encode('utf-8', 'ignore')
                dict_data['crawler_time'] = str(int(time.time())).encode('utf-8', 'ignore')

                if not dict_data['scope']:
                    dict_data['scope'] = '其他'
                dict_data['district'] = c['result']['addressComponent']['district'].encode('utf-8', 'ignore')

                if not dict_data['district']:
                    dict_data['district'] = '其他'
                dict_data_list.append(dict_data)


        cnxn = MySQLdb.connect(host=self.into_db[0], user=self.into_db[1], passwd=self.into_db[2],
                               charset=self.into_db[3])
        cursor = cnxn.cursor()

        #print len(dict_data_list)

        for x in dict_data_list :                                    # 遍历
            if not x['name'].decode('utf-8') in url_database:        # 判断小区是否已经存在
                # 插入数据
                sql = "insert into test.community_info ({}) values ({})".format(
                    ",".join([item for item in self.ziduan]),
                    ",".join([j + x[i] + j for j, i in zip(self.seq, self.ziduan)]))
                
                cursor.execute(sql)
        cnxn.commit()                                                # 同理,提交事务
        cnxn.close()                                                 # 断开连接

主程序入口:
# 主程序入口
if __name__ == '__main__':
    a = [('西安', [34.4438130000, 108.8006150000, 34.1830240000, 109.1546790000])]  # 设定西安市区经纬度范围

    into_db=("localhost","username","user_password","utf8")

    for x, y in a:
       example1 = community_info(y, x, into_db)

最后MySQL中数据样例如下:


附上原文作者链接: http://blog.csdn.net/qq_22073849/article/details/54429979




  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,我了解了你的问题。首先,你需要使用百度地图API来获取轨迹点的经纬度信息,然后使用Python的Matplotlib库来绘制轨迹图。 以下是一个简单的步骤: 1. 在百度地图开放平台上创建一个应用,获取到对应的AK(Access Key)。 2. 使用百度地图API的“轨迹纠偏”功能,将原始轨迹点转换为经纬度坐标点,并保存到本地文件中。 3. 使用Python的Matplotlib库,读取保存的经纬度坐标点,绘制轨迹图。 下面是一个简单的代码示例: ```python import requests import json import matplotlib.pyplot as plt # 设置AK ak = 'your_access_key' # 获取轨迹点 url = 'http://api.map.baidu.com/rectify/v1/track?ak={}'.format(ak) data = { "entity_name": "your_entity_name", "start_time": "your_start_time", "end_time": "your_end_time", "is_processed": "1" } response = requests.post(url, data=json.dumps(data)) points = response.json()['points'] # 保存轨迹点到本地文件 with open('points.txt', 'w') as f: for point in points: f.write('{} {}\n'.format(point['x'], point['y'])) # 绘制轨迹图 x = [] y = [] with open('points.txt', 'r') as f: for line in f.readlines(): point = line.strip().split(' ') x.append(float(point[0])) y.append(float(point[1])) plt.plot(x, y) plt.show() ``` 在上述代码中,你需要替换AK、entity_name、start_time和end_time参数为你自己的值。同时,你也可以根据需要自定义轨迹图的样式。 希望这可以帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值