我是靠谱客的博主 现实绿草,最近开发中收集的这篇文章主要介绍python 高德地图交通态势爬取(存入mysql),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 高德Web服务API提供了交通态势的http接口,使用时分为以下3个过程:

第一步,申请”Web服务API接口”密钥(Key);

第二步,拼接HTTP请求URL,第一步申请的Key需作为必填参数一同发送;

第三步,接收HTTP请求返回的数据(JSON或XML格式),解析数据。

详情参见https://lbs.amap.com/api/webservice/guide/api/trafficstatus

import requests
import json
import time
import pymysql

conn = pymysql.connect(
      host='127.0.0.1',
      port=3306,
      user='root',
      password='root',
      db='traffic_info',
    charset='utf8mb4',
)
cur=conn.cursor()
cur.execute("SELECT VERSION()")

# 使用 fetchone() 方法获取单条数据.
data=cur.fetchone()
print(data)
sql= "CREATE TABLE if not exists traffic_info(id int primary key auto_increment,road_name VARCHAR(100),status int,speed int,jingdu VARCHAR(20), weidu VARCHAR(20),time VARCHAR(20));"
cur.execute(sql)

def getjson():
    headers={
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
    }
    pa = {
        'key':'换成自己的',
        'level':5,
        'rectangle':'106.567862,29.439563;106.584466,29.524268',
        'extensions' :'all',
        'output' : 'JSON'
    }
    r = requests.get('https://restapi.amap.com/v3/traffic/status/rectangle?', params=pa, headers=headers)
    decodejson=json.loads(r.text)
    return decodejson


decodejson=getjson()
countnum=1
while countnum < 4033:
    if len(decodejson)==3:
        print("No Data")
    else:
        if decodejson['trafficinfo']['roads']:
            for each in decodejson['trafficinfo']['roads']:
                try:
                    name=each['name']
                except:
                    name=None
                try:
                    status=each['status']
                except:
                    status=None
                try:
                    lcodes=each['lcodes']
                except:
                    lcodes=None
                try:
                    direction = each['direction']
                except:
                    direction = None
                try:
                    speed = each['speed']
                except:
                    speed = None
                try:
                    polyline1 = each['polyline']
                    print(polyline1)
                    print(type(polyline1))
                    polyline = polyline1.split(";")
                    for i in range(0,len(polyline)):
                        jingwei=polyline[i].split(',')
                        dt=time.localtime()
                        ft= "%Y-%m-%d %H:%M:%S"
                        nt=time.strftime(ft,dt)
                        # sql_insert="insert into taffic_info (road_name,status,speed,jingdu,weidu,time) values (" + "'" + name + "'" + "," + "'" + status + "'" + "," + "'" + speed + "'" + "," + "'" + jingwei[0] + "'" + "," + "'" + jingwei[1] + "'" + "," + "'" + nt + "'" + ");"
                        sql_insert="insert into traffic_info (road_name,status,speed,jingdu,weidu,time) values ('%s', %s,  %s,  '%s', '%s', '%s')" % (name, status, speed, jingwei[0], jingwei[1],nt)
                        print(sql_insert)
                        cur.execute(sql_insert)
                except Exception as e:
                    print(e)
                    polyline=None
        conn.commit()
    countnum=countnum+1
    time.sleep(300)
conn.commit()
conn.close()

 

如有问题或者感兴趣,可自己申请key或者评论留言哦!!!

最后

以上就是现实绿草为你收集整理的python 高德地图交通态势爬取(存入mysql)的全部内容,希望文章能够帮你解决python 高德地图交通态势爬取(存入mysql)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部