python采集高德地图上商家信息代码(亲测OK)

项目场景:

项目场景:我需要采集本地的商户信息,获得相关的行业信息数据,分析一下。


描述

我之前有想过从企查查拿企业信息,但是我想获得更多的个体商户信息,想对当前城市做一个数据统计,分析出到底哪一行业更多,更有热度。然后可以帮我去定位到如果我去开一家店,选择什么样的行业能赚钱。这也是我需要分析的。所以,分析数据,首先需要拿到数据。


步骤流程:

高德 确实是比较好的一个平台,它有自己的API开发接口。

接口文档:API文档-开发指南-Web服务 API | 高德地图API

 以下就是代码:

import requests
import json
import datetime
import MysqlDBUtil

# types 行业号
# city 城市/区号

'''
获取城市商家信息
'''
def getData(types, city):
    # 高德地图Web API的请求URL

    num = 0;
    for i in range(3, 19):
        url = 'https://restapi.amap.com/v3/place/text'
        #计算分页
        #page = (i -1) * 20 + 1
        #offset = (i -1) * 20 + 1
        #print(offset)
        # 高德地图Web API的请求参数
        params = {
            #'key': '你的应用key',
            'key': '您的高德应用key',
            #'keywords': '酒店',  # 搜索关键字
            'types': str(types),    # 050000-餐饮服务   060000-购物服务    070000-生活服务    100000-风景名胜  170000-公司企业
             # 城市 深圳-0755
            # (罗湖区-440303   福田区-440304   南山区-440305   宝安区-440306  龙岗区-440307  盐田区-440308  龙华区-440309  坪山区-440310  光明区-440311 )
            'city': str(city),
            'citylimit': 'true', # 仅返回指定城市数据
            'output': 'JSON',  # 输出格式
            'offset': 50,   # 每页记录数据
            'page': i, # 当前页数
            'extensions': 'all' #返回结果
        }

        # 发送请求
        response = requests.get(url, params=params)

        # 处理请求结果
        if response.status_code == 200:
            data = json.loads(response.text)
            print(data)
            if data['status'] == '1':

                # if len(data['pois']) >1:
                # 提取商家信息
                for poi in data['pois']:
                    name = poi['name']   # 商铺名
                    address = poi['address']
                    location = poi['location']
                    type = poi['type']
                    pname = poi['pname']
                    cityname = poi['cityname']
                    adname = poi['adname']
                    tel = poi['tel']   #电话
                    biz_type = poi['biz_type']   #类型
                    photos = poi['photos']   #照片
                    url = ""
                    for photo in photos:
                        url = url + photo['url'] + ";";

                    biz_ext =  "'"+ str(poi['biz_ext']).replace("\n", "").replace("\'","\"") + "'"
                    print(poi['biz_ext'])
                   # biz_ext_obj =  json.loads(biz_ext)  # 营业时间
                    biz_ext_obj =  poi['biz_ext']  # 营业时间
                    biz_ext_str = "";
                    if biz_ext.find('opentime2') >= 0 :
                      biz_ext_str = biz_ext_obj['opentime2'] + ";"
                    if  biz_ext.find('open_time')>= 0:
                        biz_ext_str = biz_ext_str + biz_ext_obj['open_time'] + ";"
                    now_time = datetime.datetime.now().strftime('%Y-%m-%d')

                    itemStr = "'" + str(name) + "','" +str(address)+ "','" +str(location)+ "','" +str(type)+ "','" +str(pname)+ "','" +str(cityname)+ "','" +str(adname)+ "','" +str(tel)+ "','" + str(biz_type) + "','" +str(url)+ "','" +str(biz_ext_str)+ "','" +str(now_time)+ "'"
                    # TODO: 保存商家信息到文件或数据库中
                    print(itemStr)

                    # 保存入库
                    conn = MysqlDBUtil.MysqlDBUtil.connect_db('test')
                    num1 = MysqlDBUtil.MysqlDBUtil.insertDb(conn, 'gaode',
                                                            "name,address,location,type,pname,cityname,adname,tel,biz_type,photos,biz_ext,insert_time",
                                                            itemStr)

            else:
                print('请求失败:', data['info'])
        else:
            print('请求失败:', response.status_code)



if __name__ == '__main__':
    # str1 = '{"cost": [], "rating": "4.8", "meal_ordering": "0"}'
    #
    # str1_json = json.loads(str1);
    # ss = str1_json["rating"]
    # print(str(ss))

    # 050000-餐饮服务   060000-购物服务    070000-生活服务    100000-风景名胜  170000-公司企业
    types = "050000"
    # 城市 深圳-0755
    # (罗湖区-440303   福田区-440304   南山区-440305   宝安区-440306  龙岗区-440307  盐田区-440308  龙华区-440309  坪山区-440310  光明区-440311 )
    city = "440309"

    getData(types,city)

 数据表存储如下:

 分享下高德的文档:

高德API城市和商户信息编号文件

 需要注意: 高德的这个接口的次数每天免费次数是有限的,要是企业使用,可以购买次数即可。

 

 价格也不贵,所以,看个人的情况了。


猜你喜欢

转载自blog.csdn.net/bug_love/article/details/130348103