python - 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]>
解决方案
我认为您的 url_base 包含错误。尝试在末尾添加“.0”
base_url = "https://population.un.org/dataportalapi/api/v1.0"
推荐阅读
- android - 如何将 asm 代码块嵌入到使用 clang 编译并将在 Android 上运行的 c 程序中
- linux - bash 脚本从 bash 脚本添加 git 凭据
- php - 可以将数组添加到我的创建方法吗?
- c# - System.DllNotFoundException:部署到 Docker Linux 容器时无法加载 DLL 'oci'
- ios - 如何在carplay模拟器中启用触摸
- machine-learning - 编写输入为 (X * theta) 的 sigmoid 函数
- xamarin - 适用于 Xamarin.Android 和 Xamarin.iOS 的 Nuspec
- reactjs - React.JS 中的服务器端渲染(同构,通用)
- java - 应用 .useDelimiter 时获取异常
- go - 如何在运行时更改变量类型