初学者尝试调试python代码调用FCC API的网络中立评论

2024-09-30 06:24:00 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是Jeffrey Fossett提供的一些代码https://github.com/Fossj117/fossj117.github.io/blob/master/_code/2017-05-13-fcc-filings/final/fcc_filings_with_public_api.py

我刚刚设置了python环境并尝试运行这段代码(我在我的环境中的代码中插入了一个API键)

''' 使用Python抓取FCC文件摘要17-108的快速脚本 FCC的公共API https://www.fcc.gov/ecfs/public-api-docs.html 注: '''

import requests 
import pandas as pd

def get_filings(endpoint, offset, n_records, proceeding, api_key): 
''' 
Gets FCC filings about given proceeding from endpoint, starting 
at offset and collecting n_records (breaks if n_records too large)
'''

print "Trying to get filings {} to {}...".format(str(offset), str(offset + n_records))

payload = {'limit':n_records, 'proceedings.name': proceeding, 'offset':offset, 'api_key': api_key, "sort": "date_submission,ASC"}

r = requests.get(endpoint, params = payload)
filings = r.json()['filings']

print "...got {}, returned {} filings".format(r.reason, len(filings))

return filings


def clean_data(filings): 
''' 
Clean up the raw scraped data for analysis
'''

df = pd.DataFrame(filings)

df_filtered = df[['id_submission', 'contact_email', 'date_submission', 'date_received', 'date_disseminated','text_data', 'addressentity']]

# Extract geo data 
df_filtered['city'] = df_filtered.addressentity.apply(lambda x: x['city'] if 'city' in x.keys() else None)
df_filtered['state'] = df_filtered.addressentity.apply(lambda x: x['state'] if 'state' in x.keys() else None)
df_filtered['zip_code'] = df_filtered.addressentity.apply(lambda x: x['zip_code'] if 'zip_code' in x.keys() else None)

df_clean = df_filtered.drop(['addressentity'], axis = 1)

return df_clean

if __name__ == '__main__': 

# static params
PROCEEDING = '17-108'
ENDPOINT = 'https://publicapi.fcc.gov/ecfs/filings'

API_KEY = "" # Your API Key Here 

# initialize
OFFSET = 0
N_RECORDS = 10000 # larger than this seems to break the API

filings = []

# Main Loop
while True: 

    new_filings = get_filings(ENDPOINT, OFFSET, N_RECORDS, PROCEEDING, API_KEY)

    if new_filings: 

        filings += new_filings
        OFFSET += N_RECORDS

    else: 

        break 

# clean the data up & write it to a file for analysis
df_clean = clean_data(filings)
df_clean.to_csv('raw_data_pub_api_sorted_5_14_2AM.csv', encoding = 'utf-8')

运行此代码时,我得到以下输出:

“文件”查询.py“,第16行 打印“尝试获取文件{}到{}…”。格式(str(offset),str(offset+n_records)) ^ 语法错误:语法无效

我猜是print命令或者我调用的静态参数有语法错误?(因为那是它的断裂点)。我有点不知所措。任何帮助都将不胜感激。在


Tags: to代码cleanapidfdataifcode
2条回答

你在使用什么版本的python? 在Python3中使用: print("whatever you wanna print") 而不是 print "whatever you wanna print"

只需将print语句括在括号中,如下所示:

import requests 
import pandas as pd

def get_filings(endpoint, offset, n_records, proceeding, api_key): 
    ''' 
    Gets FCC filings about given proceeding from endpoint, starting 
    at offset and collecting n_records (breaks if n_records too large)
    '''

    print("Trying to get filings {} to {}...".format(str(offset), str(offset + n_records)))

    payload = {'limit':n_records, 'proceedings.name': proceeding, 'offset':offset, 'api_key': api_key, "sort": "date_submission,ASC"}

    r = requests.get(endpoint, params = payload)
    filings = r.json()['filings']

    print("...got {}, returned {} filings".format(r.reason, len(filings)))

    return filings


def clean_data(filings): 
    ''' 
    Clean up the raw scraped data for analysis
    '''

    df = pd.DataFrame(filings)

    df_filtered = df[['id_submission', 'contact_email', 'date_submission', 'date_received', 'date_disseminated','text_data', 'addressentity']]

    # Extract geo data 
    df_filtered['city'] = df_filtered.addressentity.apply(lambda x: x['city'] if 'city' in x.keys() else None)
    df_filtered['state'] = df_filtered.addressentity.apply(lambda x: x['state'] if 'state' in x.keys() else None)
    df_filtered['zip_code'] = df_filtered.addressentity.apply(lambda x: x['zip_code'] if 'zip_code' in x.keys() else None)

    df_clean = df_filtered.drop(['addressentity'], axis = 1)

    return df_clean

if __name__ == '__main__': 
    # static params
    PROCEEDING = '17-108'
    ENDPOINT = 'https://publicapi.fcc.gov/ecfs/filings'

    API_KEY = "rVFHpkCgR2oigr9vQmJREnrSUVtaJC1NIiMgYL8S" # Your API Key Here 

    # initialize
    OFFSET = 0
    N_RECORDS = 10000 # larger than this seems to break the API

    filings = []

    # Main Loop
    while True: 

        new_filings = get_filings(ENDPOINT, OFFSET, N_RECORDS, PROCEEDING, API_KEY)

        if new_filings: 
            filings += new_filings
            OFFSET += N_RECORDS
        else: 
            break 

    # clean the data up & write it to a file for analysis
    df_clean = clean_data(filings)
    df_clean.to_csv('raw_data_pub_api_sorted_5_14_2AM.csv', encoding = 'utf-8')

这对我很管用,尽管我的电脑在收到160K文件后内存不足:)

相关问题 更多 >

    热门问题