抓取GLEIF信息:使用pygleif、leipy查询LEI数据连接报错如何解决
问题根因分析
- 你使用的两个第三方库
leipy、pygleif均已停止维护3年以上,内置的API请求地址是GLEIF旧版已下线的接口,所以会触发连接失败、502报错 - 部分地区网络访问GLEIF公共接口存在隧道代理异常,也会触发502 bad gateway错误
解决方案
废弃停止维护的第三方库,直接调用GLEIF最新版公开API即可正常获取数据。如果需要批量补全超过1000条LEI的信息,建议直接下载GLEIF官方每日更新的全量数据包本地解析,效率远高于接口调用。
可用示例代码(Python)
import requests import pandas as pd # 单个LEI查询示例 def query_single_lei(lei_code): base_url = f"https://api.gleif.org/api/v1/lei-records/{lei_code}" headers = { "Accept": "application/vnd.api+json" } # 若本地有代理配置,可在get参数中加proxies={}指定正确代理 response = requests.get(base_url, headers=headers) if response.status_code == 200: return response.json() else: print(f"查询失败,状态码:{response.status_code}") return None # 测试调用 lei_data = query_single_lei("8RS0AKOLN987042F2V04") if lei_data: # 提取初始注册日期 initial_reg_date = lei_data['data']['attributes']['registration']['initialRegistrationDate'] print(initial_reg_date) # 批量查询示例(一次最多可查100个LEI) def query_batch_lei(lei_list): base_url = "https://api.gleif.org/api/v1/lei-records" params = { "filter[lei]": ",".join(lei_list), "page[size]": 100 } headers = { "Accept": "application/vnd.api+json" } response = requests.get(base_url, params=params, headers=headers) if response.status_code == 200: return pd.json_normalize(response.json()['data']) else: print(f"批量查询失败,状态码:{response.status_code}") return None # 批量测试 batch_df = query_batch_lei(["HWUPKR0MPOU8FGXBT394", "8RS0AKOLN987042F2V04"]) print(batch_df[['attributes.lei', 'attributes.registration.initialRegistrationDate']])
异常处理补充
- 若仍出现502错误,检查本地网络代理配置,可尝试关闭全局代理或在requests请求中指定正确的代理参数
- 接口调用频率限制为每秒最多10次请求,批量查询时控制频率避免被临时封禁
内容的提问来源于stack exchange,提问作者Tiffany Zucman
相关产品推荐
相关产品推荐

