pandas-datareader 世界银行 API 损坏

pandas-datareader World Bank API broken

为什么世界银行 API 的最新示例不适用于 pandas-datareader

https://pandas-datareader.readthedocs.io/en/latest/remote_data.html#remote-data-wb

from pandas_datareader import wb

matches = wb.search('gdp.*capita.*const')
dat = wb.download(indicator='NY.GDP.PCAP.KD', country=['US', 'CA', 'MX'], start=2005, end=2008)
print(dat)

给我这个:

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "C:\Python36\lib\site-packages\urllib3\connection.py", line 326, in connect
    ssl_context=context)
  File "C:\Python36\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Python36\lib\ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "C:\Python36\lib\ssl.py", line 814, in __init__
    self.do_handshake()
  File "C:\Python36\lib\ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "C:\Python36\lib\ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Python36\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.worldbank.org', port=443): Max retries exceeded with url: /v2/indicators?per_page=50000&format=json (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Jason/Google Drive/pycharm/test.py", line 3, in <module>
    matches = wb.search('gdp.*capita.*const')
  File "C:\Python36\lib\site-packages\pandas_datareader\wb.py", line 938, in search
    return WorldBankReader(**kwargs).search(string=string, field=field, case=case)
  File "C:\Python36\lib\site-packages\pandas_datareader\wb.py", line 809, in search
    indicators = self.get_indicators()
  File "C:\Python36\lib\site-packages\pandas_datareader\wb.py", line 745, in get_indicators
    resp = self._get_response(url)
  File "C:\Python36\lib\site-packages\pandas_datareader\base.py", line 155, in _get_response
    response = self.session.get(url, params=params, headers=headers)
  File "C:\Python36\lib\site-packages\requests\sessions.py", line 546, in get
    return self.request('GET', url, **kwargs)
  File "C:\Python36\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python36\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python36\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.worldbank.org', port=443): Max retries exceeded with url: /v2/indicators?per_page=50000&format=json (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)'),))

它曾经在 0.7 版本上工作。我已经 运行 一年左右没用了,今天 运行 它给了我同样的错误,所以我升级到最新的数据读取器版本,但它仍然无法工作。

我没有解决办法。但它在星期五工作。世界银行最近更新了他们的证书,所以这可能就是原因。我在我的应用程序中使用世界银行数据,所以卡在这里。

我已在 pandas_datareader github 页面提出请求:https://github.com/pydata/pandas-datareader/issues/791

我相信我们这里有多个问题,

1- 指标假定在数组 ['NY.GDP.PCAP.KD']

 dat = wb.download(indicator=['NY.GDP.PCAP.KD'], country=['US', 'CA', 'MX'], start=2005, end=2008)
    print(dat)

2- 现在来到主要问题,让我们解决它并直接在世界银行数据上访问您的 url,在这里尝试,它正在工作,所以 pandas_datareader SSL 的问题(也许套接字客户端)需要更新。

https://api.worldbank.org/v2/countries/US;CA;MX/indicators/NY.GDP.PCAP.KD?date=2005%3A2008&per_page=25000&format=json

3- 除了 SSL 问题之外,我还遇到了另一个问题,这与世界银行对数据大小的一些新限制有关,我还需要更多调查来确认这一点。尽管如此,它仍然存在(这可能与他们的新证书有关 URL 长度限制,可能是,请在下面查看。

===更新===

经测试证实,有国家数限制 尝试删除任何国家以将数量减少到 65,这将起作用

https://api.worldbank.org/v2/countries/AFG;AGO;ARE;AUS;AUT;AZE;BEL;BGD;BHR;BRA;CAN;CHE;CHN;CZE;DEU;DNK;DZA;EGY;ESP;FIN;FRA;GBR;GHA;HKG;HUN;IDN;IND;IRL;IRN;IRQ;ITA;JOR;JPN;KAZ;KEN;KOR;KWT;LBN;LBY;LKA;MAR;MYS;NGA;NLD;OMN;PAK;PHL;POL;QAT;RUS;SAU;SDN;SGP;SOM;SWE;SYR;THA;TUR;TWN;TZA;UGA;UKR;USA;VNM;YEM;ZAF/indicators/AG.LND.ARBL.ZS?date=2005%3A2008&per_page=25000&format=json

===== 更新 26-06-2020 ====

至于今天上面的 link 又开始工作了,看来他们回应了我上周的票。