python爬取交通网站json数据

刚学python的新手一个,小问题比较多,大家凑活看看
我们爬取的网站是 https://jiaotong.baidu.com/trafficindex/city/list
直接贴上我的代码:

import requests
import csv
import json

#获取网页的信息
url = 'https://jiaotong.baidu.com/trafficindex/city/list'
html = requests.get(url).text

#获取网页中需要的信息,这里我查了好多资料才知道如何爬取这种形式的json...
data = json.loads(html)
datas = data['data']['list']

#创建csv文件,查了好多资料不知道如何转CSV时没有乱码,按照网上说的先转为txt,我就已经是乱码了,试了好多方法都不行...
csvFile = open('E:/files/jiaotong.csv','w+',newline='',encoding='gbk')
writer = csv.writer(csvFile)
fieldnames = ['time','citycode','cityname','index','last_index','index_level','speed','city_coords','provincecode','provincename','weekRate']
writer = csv.DictWriter(csvFile,fieldnames=fieldnames)
writer.writeheader()
for d in datas:
    writer.writerow({
        'time':d['time'],
        'citycode':d['citycode'],
        'cityname':d['cityname'],
        'index':d['index'],
        'last_index':d['last_index'],
        'index_level':d['index_level'],
        'speed':d['speed'],
        'city_coords':d['city_coords'],
        'provincecode':d['provincecode'],
        'provincename':d['provincename'],
        'weekRate':d['weekRate']
        })
csvFile.close()

你可能感兴趣的:(python爬取交通网站json数据)