首页 > 解决方案 > httpsconnectionpool 提取 api

问题描述

嗨,我正在尝试运行此代码,但它不工作我收到此错误消息

HTTPSConnectionPool(host='population.un.org', port=443): 最大重试次数超出 url: /dataportalapi/api/v1/locations/ (由 ProxyError('Cannot connect to proxy.', ConnectionResetError(10054, '现有连接被远程主机强行关闭',无,10054,无)))


    import pandas as pd
    import json
    import requests

    # Declares the base URL for calling API
    base_url = "https://population.un.org/dataportalapi/api/v1"

    # Creates the target URL, indicators, in this instance
    target = base_url + "/locations/"

    # Calls the API
    response = requests.get(target)

    # Converts call into JSON
    j = response.json()

    # Converts JSON into a pandas DataFrame.
    df = pd.json_normalize(j) # pd.json_normalize flattens the JSON to accomodate nested lists within the JSON structure

    # Alternatively, a user can have the same results returned in a CSV format and import them directly using pandas.read_csv()
    df2 = pd.read_csv(target+"?format=csv")

#To view response code:

    print(response)

    ## <Response [200]>

标签: pythonpandasapirequest

解决方案


我认为您的 url_base 包含错误。尝试在末尾添加“.0”

base_url = "https://population.un.org/dataportalapi/api/v1.0"


推荐阅读