使用BeautifulSoup提取LEI页面指定字段并写入DataFrame方法咨询
问题根因
你当前请求的是前端单页应用的渲染地址,LEI实体数据都是页面加载完成后由JS动态请求接口、再渲染到页面上的,你用requests拿到的只是没有业务数据的空页面骨架,自然无法匹配到需要的字段。
最优实现方案
不需要解析HTML,直接调用站点后端的数据接口拿结构化的JSON数据,一步就能提取到需要的字段,比BeautifulSoup解析页面简单很多,完整实现代码如下:
import requests import pandas as pd # 关闭verify=False带来的SSL警告 requests.packages.urllib3.disable_warnings() # 配置要查询的LEI编码和请求头 lei_code = "335800NV2ZZ3157KUJ54" api_url = f"https://www.lei-lookup.com/rest/record/{lei_code}" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" } # 请求接口拿结构化数据 resp = requests.get(api_url, headers=headers, verify=False) lei_data = resp.json() # 提取你需要的所有字段 result = { "Legal Name": lei_data.get("entity", {}).get("legalName", {}).get("name"), "Registered At": lei_data.get("registration", {}).get("initialRegistrationDate"), "Registration Authority": lei_data.get("registration", {}).get("registrar", {}).get("name"), "Entity ID": lei_data.get("lei"), "Legal Jurisdiction": lei_data.get("entity", {}).get("jurisdiction"), "Entity Legal Form Code": lei_data.get("entity", {}).get("legalForm", {}).get("code"), "Entity Status": lei_data.get("registration", {}).get("status") } # 转成DataFrame df = pd.DataFrame([result]) # 批量查询的话把所有result字典放到同一个列表里,再传给pd.DataFrame即可 print(df)
补充说明
如果你必须要用BeautifulSoup解析页面内容,需要先使用Selenium、Playwright等工具模拟浏览器加载完整页面、等待动态内容渲染完成后再拿页面源码交给BeautifulSoup处理,效率远低于直接调用接口,不推荐使用。
内容的提问来源于stack exchange,提问作者Tiffany Zucman
相关产品推荐
相关产品推荐

