You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python网页抓取失败:无法获取FDIC网站Description栏文本

获取FDIC网站「Description」板块内容的解决方案

你遇到的问题根源在于目标网站是JavaScript渲染的单页应用(SPA):用requests请求得到的只是页面的静态HTML骨架,而「Description」这类动态数据是页面加载后通过AJAX请求异步获取的,所以BeautifulSoup根本找不到目标元素。

下面提供两种可行的解决方法:

方法一:直接调用后端API接口(推荐)

通过浏览器开发者工具的「网络」面板,可以找到加载详情数据的真实API接口,直接请求该接口就能拿到结构化的JSON数据,无需处理页面渲染。

示例代码:

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
    "Accept": "application/json"
}
# 替换为你在开发者工具中找到的真实API地址,以下为示例
api_url = "https://www7.fdic.gov/DICT/app/api/v1/financial-institutions/AATOTFV"
response = requests.get(api_url, headers=headers)
data = response.json()

# 根据实际返回的JSON结构提取Description字段,示例如下
description = data.get("description")  # 若结构嵌套,可改为data["xxx"]["description"]
print(description)

方法二:用Selenium模拟浏览器渲染

如果找不到API接口,或者页面逻辑复杂,可以用Selenium模拟真实浏览器加载页面,等待JavaScript渲染完成后再提取元素。

示例代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

url = "https://www7.fdic.gov/DICT/app/templates/Index.html#!/Details/AATOTFV/FTS/FI"

# 自动安装Chrome驱动并初始化浏览器
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get(url)

# 等待页面动态内容加载完成,时间可根据实际情况调整
time.sleep(3)

# 定位目标元素并提取文本
div_element = driver.find_element(By.CLASS_NAME, "data-col-custom")
print(div_element.text)

# 关闭浏览器
driver.quit()

两种方法对比

  • API方法:速度快、资源占用低,是最优解,但需要找到正确的接口地址和请求参数。
  • Selenium方法:通用性强,适合所有SPA页面,但运行速度慢,需要安装浏览器和驱动。

内容的提问来源于stack exchange,提问作者Debmallya Chanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 12:35:12