利用美国劳工局的API获取数据

获取数据有多种方式,有API最好了!没有的时候才使用爬虫

下面是利用美国劳工局的API获取数据,例子代码其实官方给的.

但是官网给的并不能运行,有缩进错误等问题.下面给出我调试后的代码

1.获取格式化的数据最重要

2.,json_pretty()里的代码,只处理了一部分数据.建议根据需要自己写

3.三个data_dict都是有效的,是三种查询方式

# -*- coding:utf-8 -*-
#这是美国劳工局的API,原来的代码在if 'M01' <= period <= 'M12':这一行被引号引起来了,导致出错
#来源 https://www.bls.gov/developers/api_python.htm#python2
#如何获得所需数据的 seriesid: https://www.bls.gov/help/hlpforma.htm
''' 通过seriesid代码的修改,我们可以得到下面这些方面的数据
    Employment & Unemployment
    Inflation & Prices
    Spending & Time Use
    Pay & Benefits
    Productivity
    Workplace Injuries
    Occupational Requirements
    International
'''
import requests
import json
import prettytable
headers = {'Content-type': 'application/json'}
# data_dict = {"seriesid": ['CUUR0000SA0','SUUR0000SA0'],"startyear":"2011", "endyear":"2014"}
# data_dict ={"seriesid":["LAUCN040010000000005", "LAUCN040010000000006"],\
#             "startyear":"2010",   "endyear":"2012",  "catalog":False,\
#             "calculations":True,  "annualaverage":True}

data_dict = {"seriesid":['LAUCN040010000000005']}
#官网提供的例子中registrationkey是一个已经不存在的key,不要这个就好了
data = json.dumps(data_dict)

p = requests.post('https://api.bls.gov/publicAPI/v2/timeseries/data/', data=data, headers=headers)
json_data = json.loads(p.text)

json_pretty()
def json_pretty():
    #用于将json数据表格化,其实重点还是获取想要的json数据,不是这个函数
    for series in json_data['Results']['series']:
        x=prettytable.PrettyTable(["series id","year","period","value","footnotes"])
        seriesId = series['seriesID']
        for item in series['data']:
            year = item['year']
            period = item['period']
            value = item['value']
            footnotes=""
            for footnote in item['footnotes']:
                if footnote:
                    footnotes = footnotes + footnote['text'] + ','
            if 'M01' <= period <= 'M12':
                x.add_row([seriesId,year,period,value,footnotes[0:-1]])
        # output = open(seriesId + '.txt','w')
        output = open(seriesId + '_ceshi.txt', 'w')
        output.write (x.get_string())
        output.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
美国劳工部官方统计数据美国劳工部统计司发布,是美国重要经济统计指标的当下和历史数据数据以文本文档形式组织,每一类指标数据由两个英文开头的文件夹构成。具体指标目录及名称如下所示。 # 英文名称 中文名称 AP Average Price Data 平均价格数据 BD Business Employment Dynamics 商业就业动态 BG Collective Bargaining-State and Local Government 集体谈判国家与地方政府 BP Collective Bargaining-Private Sector 集体谈判私营部门 CC Employer Costs for Employee Compensation (SIC) 雇员补偿的雇主成本(SIC) CD Occupational Injuries and Illness - Characteristics Data (SIC) 职业伤害和疾病-特征数据(SIC) CE Employment, Hours, and Earnings-National (NAICS) 就业,小时,盈余国家(NAICS) CF Census of Fatal Occupational Injuries (1992-2002) 普查致命职业伤害(1992-2002) CH Nonfatal cases involving days away from work: selected characteristics 非致死性案件工作天了:选择的特征 CI Employment Cost Index (NAICS) 就业成本指数(NAICS) CM Employer Costs for Employee Compensation (NAICS) 雇员报酬的单位成本(NAICS) CS Occupational Injuries and Illnesses - Characteristics Data 职业损伤和疾病-特征数据 CU Consumer Price Index-All Urban Consumers (Current Series) 所有城市消费者的消费价格指数(当前系列) CW Consumer Price Index-Urban Wage Earners and Clerical Workers (Current Series) 消费者物价指数城市工薪阶层和文职人员(当前系列) CX Consumer Expenditure Survey 消费支出调查 EB Employee Benefits Survey 员工福利调查 EC Employment Cost Index 就业成本指数 EE Employment, Hours, and Earnings-National (SIC) 就业,工时和收入国家(SIC) EI International Price Index 国际价格指数 FI Census of Fatal Occupational Injuries 致命职业伤害普查 FM Marital and Family Labor Force Statistics 婚姻和家庭劳动力统计 FW Census of Fatal Occupational Injuries 致命职业伤害普查 GG Green Goods and Services 绿色商品与服务 GP Geographic Profile 地理概况 HC Occupational Injuries and Illness - Characteristics Data (NAICS) 职业伤害和疾病特征的数据(NAICS) HS Occupational Injury and Illness Rates (based on 1972 SIC codes) 职业损伤和疾病率(基于1972 SIC代码) II Occupational Injuries and Illnesses Industry Data 职业伤害与疾病行业数据 IN International Labor Statistics 国际劳工统计 IP Industry Productivity and Costs 产业生产力与成本 JL Job Openings and Labor Turnover Survey (SIC) 职位空缺和劳动周转调查(SIC) JT Job Openings and Labor Turnover Survey (NAICS) 职位空缺和劳动力流动调查(NAICS) LA Local Area Unemployment Statistics 地区失业统计 LE Weekly and
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值