百度地图API获取数据

目前,大厂的服务范围越来越广,提供的数据信息也是比较全的,在生活服务,办公领域,人工智能等方面都全面覆盖,相对来说,他们的用户基数大,通过用户获取的信息也是巨大的。除了百度提供api,国内提供免费API获取数据的还有很多,包括新浪、豆瓣电影、饿了么、阿里、腾讯等今天使用百度地图API来请求我们想要的数据。

第一步.注册百度开发者账号

注册成功后就可以获取到应用服务AK也就是API秘钥,这个是最重要的,应用名称可以随便取,如果是普通用户一天只有2000调用限额,认证用户一天有10万次调用限额

在百度地图web服务API文档中我可以看见提供的接口和相关参数,其中就有我们要获取的AK参数,使用的GET请求

一.下面我们尝试使用API获取获取北京市的城市公园数据,需要配置参数

import requests
import json

def getjson(loc):
    headers = {‘User-Agent‘ : ‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘}
    pa = {
        ‘q‘: ‘公园‘,
        ‘region‘: loc,
        ‘scope‘: ‘2‘,
        ‘page_size‘: 20,
        ‘page_num‘: 0,
        ‘output‘: ‘json‘,
        ‘ak‘: ‘填写自己的AK‘
    }
    r = requests.get("http://api.map.baidu.com/place/v2/search", params=pa, headers= headers)
    decodejson = json.loads(r.text)
    return decodejson

getjson(‘北京市‘)

二.获取所有拥有公园的城市

import requests
import json
def getjson(loc):
    headers = {‘User-Agent‘ : ‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘}
    pa = {
        ‘q‘: ‘公园‘,
        ‘region‘: loc,
        ‘scope‘: ‘2‘,
        ‘page_size‘: 20,
        ‘page_num‘: 0,
        ‘output‘: ‘json‘,
        ‘ak‘: ‘填写自己的AK‘
    }
    r = requests.get("http://api.map.baidu.com/place/v2/search", params=pa, headers= headers)
    decodejson = json.loads(r.text)
    return decodejson

province_list = [‘江苏省‘, ‘浙江省‘, ‘广东省‘, ‘福建省‘, ‘山东省‘, ‘河南省‘, ‘河北省‘, ‘四川省‘, ‘辽宁省‘, ‘云南省‘,
                 ‘湖南省‘, ‘湖北省‘, ‘江西省‘, ‘安徽省‘, ‘山西省‘, ‘广西壮族自治区‘, ‘陕西省‘, ‘黑龙江省‘, ‘内蒙古自治区‘,
                 ‘贵州省‘, ‘吉林省‘, ‘甘肃省‘, ‘新疆维吾尔自治区‘, ‘海南省‘, ‘宁夏回族自治区‘, ‘青海省‘, ‘西藏自治区‘]
for eachprovince in province_list:
    decodejson = getjson(eachprovince)
    #print(decodejson["results"])
    for eachcity in decodejson.get(‘results‘):
        print(eachcity)
        city = eachcity[‘name‘]
        #print(city)
        num = eachcity[‘num‘]
        output = ‘\t‘.join([city, str(num)]) + ‘\r\n‘
        with open(‘cities.txt‘, "a+", encoding=‘utf-8‘) as f:
            f.write(output)
            f.close()
import requests
import json
def getjson(loc):
    headers = {‘User-Agent‘ : ‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘}
    pa = {
        ‘q‘: ‘公园‘,
        ‘region‘: loc,
        ‘scope‘: ‘2‘,
        ‘page_size‘: 20,
        ‘page_num‘: 0,
        ‘output‘: ‘json‘,
        ‘ak‘: ‘填写自己的AK‘
    }
    r = requests.get("http://api.map.baidu.com/place/v2/search", params=pa, headers= headers)
    decodejson = json.loads(r.text)
    return decodejson

decodejson = getjson(‘全国‘)
six_cities_list = [‘北京市‘,‘上海市‘,‘重庆市‘,‘天津市‘,‘香港特别行政区‘,‘澳门特别行政区‘,]
for eachprovince in decodejson[‘results‘]:
    city = eachprovince[‘name‘]
    num = eachprovince[‘num‘]
    if city in six_cities_list:
        output = ‘\t‘.join([city, str(num)]) + ‘\r\n‘
        with open(‘cities789.txt‘, "a+", encoding=‘utf-8‘) as f:
            f.write(output)
            f.close()

保存到文件

三.获取所有城市的公园数据,在从各个城市获取数据之前,先在MySQL建立baidumap数据库,用来存放所有的数据

#coding=utf-8
import pymysql

conn= pymysql.connect(host=‘localhost‘ , user=‘root‘, passwd=‘*******‘, db =‘baidumap‘, charset="utf8")
cur = conn.cursor()
sql = """CREATE TABLE city (
         id INT NOT NULL AUTO_INCREMENT,
         city VARCHAR(200) NOT NULL,
         park VARCHAR(200) NOT NULL,
         location_lat FLOAT,
         location_lng FLOAT,
         address VARCHAR(200),
         street_id VARCHAR(200),
         uid VARCHAR(200),
         created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
         PRIMARY KEY (id)
         );"""
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
city_list = list()
with open("cities.txt", ‘r‘, encoding=‘utf-8‘) as txt_file:
    for eachLine in txt_file:
        if eachLine != "" and eachLine != "\n":
            fields = eachLine.split("\t")
            city = fields[0]
            city_list.append(city)
    txt_file.close()   

#接下来爬取每个城市的数据,并将其加入city数据表中
import requests
import json
import pymysql

conn= pymysql.connect(host=‘localhost‘ , user=‘root‘, passwd=‘********‘, db =‘baidumap‘, charset="utf8")
cur = conn.cursor()

def getjson(loc,page_num):
    headers = {‘User-Agent‘ : ‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘}
    pa = {
        ‘q‘: ‘公园‘,
        ‘region‘: loc,
        ‘scope‘: ‘2‘,
        ‘page_size‘: 20,
        ‘page_num‘: page_num,
        ‘output‘: ‘json‘,
        ‘ak‘: ‘填写自己的AK‘
    }
    r = requests.get("http://api.map.baidu.com/place/v2/search", params=pa, headers= headers)
    decodejson = json.loads(r.text)
    return decodejson

for eachcity in city_list:
    not_last_page = True
    page_num = 0
    while not_last_page:
        decodejson = getjson(eachcity, page_num)
        #print (eachcity, page_num)
        if decodejson[‘results‘]:
            for eachone in decodejson[‘results‘]:
                try:
                    park = eachone[‘name‘]
                except:
                    park = None
                try:
                    location_lat = eachone[‘location‘][‘lat‘]
                except:
                    location_lat = None
                try:
                    location_lng = eachone[‘location‘][‘lng‘]
                except:
                    location_lng = None
                try:
                    address = eachone[‘address‘]
                except:
                    address = None
                try:
                    street_id = eachone[‘street_id‘]
                except:
                    street_id = None
                try:
                    uid = eachone[‘uid‘]
                except:
                    uid = None
                sql = """INSERT INTO baidumap.city
                (city, park, location_lat, location_lng, address, street_id, uid)
                VALUES
                (%s, %s, %s, %s, %s, %s, %s);"""

                cur.execute(sql, (eachcity, park, location_lat, location_lng, address, street_id, uid,))
                conn.commit()
            page_num += 1
        else:
            not_last_page = False
cur.close()
conn.close()

四.获取所有公园的详细信息

baidumap数据库已经有了city这个表格,存储了所有城市的公园数据,但是这个数据比较粗糙,接下来我们使用百度地图检索服务获取没一个公园的详情

#coding=utf-8
import pymysql

conn= pymysql.connect(host=‘localhost‘ , user=‘root‘, passwd=‘*******‘, db =‘baidumap‘, charset="utf8")
cur = conn.cursor()
sql = """CREATE TABLE park (
         id INT NOT NULL AUTO_INCREMENT,
         park VARCHAR(200) NOT NULL,
         location_lat FLOAT,
         location_lng FLOAT,
         address VARCHAR(200),
         street_id VARCHAR(200),
         telephone VARCHAR(200),
         detail INT,
         uid VARCHAR(200),
         tag VARCHAR(200),
         type VARCHAR(200),
         detail_url VARCHAR(800),
         price INT,
         overall_rating FLOAT,
         image_num INT,
         comment_num INT,
         shop_hours VARCHAR(800),
         alias VARCHAR(800),
         keyword VARCHAR(800),
         scope_type VARCHAR(200),
         scope_grade VARCHAR(200),
         description VARCHAR(9000),
         created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
         PRIMARY KEY (id)
         );"""
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
import requests
import json
import pymysql

conn= pymysql.connect(host=‘localhost‘ , user=‘root‘, passwd=‘********‘, db =‘baidumap‘, charset="utf8")
cur = conn.cursor()
sql = "Select uid from baidumap.city where id > 0;"

cur.execute(sql)
conn.commit()
results = cur.fetchall()
cur.close()
conn.close()
import requests
import json
import pymysql

conn= pymysql.connect(host=‘localhost‘ , user=‘root‘, passwd=‘********‘, db =‘baidumap‘, charset="utf8")
cur = conn.cursor()
sql = "Select uid from baidumap.city where id > 0;"

cur.execute(sql)
conn.commit()
results = cur.fetchall()
#print(results)

def getjson(uid):
    headers = {‘User-Agent‘ : ‘Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘}
    pa = {
        ‘uid‘: uid,
        ‘scope‘: ‘2‘,
        ‘output‘: ‘json‘,
        ‘ak‘: ‘填写自己的AK‘
    }
    r = requests.get("http://api.map.baidu.com/place/v2/search", params=pa, headers= headers)
    decodejson = json.loads(r.text)
    return decodejson

for row in results:
    print(row)
    uid = row[0]
    decodejson = getjson(uid)
    #print (uid)
    info = decodejson[‘result‘]
    try:
        park = info[‘name‘]
    except:
        park = None
    try:
        location_lat = info[‘location‘][‘lat‘]
    except:
        location_lat = None
    try:
        location_lng = info[‘location‘][‘lng‘]
    except:
        location_lng = None
    try:
        address = info[‘address‘]
    except:
        address = None
    try:
        street_id = info[‘street_id‘]
    except:
        street_id = None
    try:
        telephone = info[‘telephone‘]
    except:
        telephone = None
    try:
        detail = info[‘detail‘]
    except:
        detail = None
    try:
        tag = info[‘detail_info‘][‘tag‘]
    except:
        tag = None
    try:
        detail_url = info[‘detail_info‘][‘detail_url‘]
    except:
        detail_url = None
    try:
        type = info[‘detail_info‘][‘type‘]
    except:
        type = None
    try:
        overall_rating = info[‘detail_info‘][‘overall_rating‘]
    except:
        overall_rating = None
    try:
        image_num = info[‘detail_info‘][‘image_num‘]
    except:
        image_num = None
    try:
        comment_num = info[‘detail_info‘][‘comment_num‘]
    except:
        comment_num = None
    try:
        key_words = ‘‘
        key_words_list = info[‘detail_info‘][‘di_review_keyword‘]
        for eachone in key_words_list:
            key_words = key_words + eachone[‘keyword‘] + ‘/‘
    except:
        key_words = None
    try:
        shop_hours = info[‘detail_info‘][‘shop_hours‘]
    except:
        shop_hours = None
    try:
        alias = info[‘detail_info‘][‘alias‘]
    except:
        alias = None
    try:
        scope_type = info[‘detail_info‘][‘scope_type‘]
    except:
        scope_type = None
    try:
        scope_grade = info[‘detail_info‘][‘scope_grade‘]
    except:
        scope_grade = None
    try:
        description = info[‘detail_info‘][‘description‘]
    except:
        description = None
    sql = """INSERT INTO baidumap.park
    (park, location_lat, location_lng, address, street_id, uid, telephone, detail, tag, detail_url, type, overall_rating, image_num,
    comment_num, keyword, shop_hours, alias, scope_type, scope_grade, description)
    VALUES
    (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"""

    cur.execute(sql, (park, location_lat, location_lng, address, street_id, uid, telephone, detail, tag, detail_url,
                      type, overall_rating, image_num, comment_num, key_words, shop_hours, alias, scope_type, scope_grade, description,))
    conn.commit()
cur.close()
conn.close()

原文地址:https://www.cnblogs.com/jzxs/p/10795246.html

时间: 2024-11-01 01:18:30

百度地图API获取数据的相关文章

HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地(转)

HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地 我是应用在微信中,自定义菜单,菜单直接链接到这个HTML5页面,获取当前位置后,页面中定好目的地,这样打开页面后直接进入导航页面 可以省下先发送位置信息后,点确定再出导航,省一步, <!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"> <meta name="viewpor

根据百度地图API获取指定地点的经纬度

做项目时,遇到对地点获取地图中对应的经纬度,作一下笔记,以备以后直接使用 package com.hpzx.data; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.n

HTML5 利用百度地图API获取当前位置

由于项目需要定位到城市,研究了地理定位,做了一些手记,和大家分享一下~ 项目的开发需求是获取到当前用户的位置,然后为用户提供一些服务. 此时可以采用两种定位方式,一种是用GPS的定位,然后将定位到的经纬度传递到百度地图的API接口当中.另一种方法是利用百度API接口,使用IP地址定位,然后使用IP地址获取到的经纬度,传递到百度地图API接口中. 自己尝试使用了这两种方式进行定位.相对来说,IP地址定位,偏差比较大.GPS定位虽然还可以,但是也不足够精确.产生比较大偏差的原因有可能是以下几点造成的

PhoneGap Geolocation结合百度地图api获取地理位置api

一.使用百度地图API 1.地址:http://developer.baidu.com/map/ 2.在js DEMO中获取反地址解析的DEMO 3.修改这个DEMO的密钥,去创建应用就能创建密钥,然后复制密钥到这个页面即可 4.使用PhoneGap Geolocation 获取地理位置获取到的经度和纬度赋值给point即可 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <

利用百度地图API获取当前位置信息

利用百度地图API可以做很多事情,个人感觉最核心也是最基础的就是定位功能了.这里分享一个制作的JS可以实现登录网页后定位: 1 <script type="text/javascript"> 2 var map; 3 var gpsPoint; 4 var baiduPoint; 5 var gpsAddress; 6 var baiduAddress; 7 var x; 8 var y; 9 function getLocation() { 10 //根据IP获取城市 1

百度统计api获取数据(前端调用)

需求场景 想要了解每天多少人访问了网站,多少个新增用户,地域分布,点击了哪些页面,停留了多久,等等... 国内用的最多的就是百度统计吧,傻瓜式的注册然后插一段代码到项目里就行了. 最近也在自己的博客里使用了百度统计,但是当想要获取这些数据时,看到官方文档,简直想骂人.网上也不是没有代码示例,但清一色的都是java代码,而官网给出的demo也是php,这是要逼死前端吗? 来吧,直接上代码: 1.  获取站点    https://api.baidu.com/json/tongji/v1/Repor

Python通过百度地图API获取地址的经纬度

API 地址:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding 一个简单的API,可以通过地址(地名)获得经纬度,或者通过经纬度获取周边的位置信息. ak是百度开发者APIKEY,注册地址:http://developer.baidu.com/ #!/usr/bin/env python3 import requests import json def locatebyAddr(add

利用百度地图API获取用户浏览器所在省市区

好久没用过h5新api地理定位--navigator.geolocation, 就在前几天,,, 发现,不能用了???(谷歌被墙了,陷入悲伤) 整个人都懵逼,项目需要定位啊,,,懵逼,,, 然后就写了三天,tm的写不出来,今天终于,,, 直接上代码咯. <!doctype html><html><head> <meta charset="utf-8"> <title>标题</title> <meta name

百度地图api获取经纬度和城市名称

想集成百度API定位需要完成以下步骤: 1:注册开发者 2:申请key 3:下载jar包 4:代码集成 先看下效果: 1:注册开发者 最好提前申请,审核需要1,2天 2:申请key 本人用的是Android Studio,所以用命令行查看SHA1码 3:下载jar包 只需下载"定位功能"的开发包即可 4:代码集成(详细可见官方讲解,以下代码以简洁实用为主) package com.union.roid.testroid; import android.app.Activity; imp