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

使用pandas爬取OTC Markets股票筛选器网站出现No table found错误求助

问题解决:OTC Markets股票筛选器表格爬取失败

报错原因

该站点的股票列表表格由JavaScript动态渲染生成,你通过requests直接请求得到的初始HTML源码中不存在完整的<table>标签结构,因此pandas.read_html()无法识别到表格,触发ValueError: No tables found报错。

解决方案1:使用Selenium模拟浏览器渲染(适合简单调试、页面结构简单的场景)

  • 依赖安装:执行pip install selenium webdriver-manager安装所需库
  • 代码示例:
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 初始化浏览器
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.otcmarkets.com/research/stock-screener")

# 等待表格加载完成,最多等待10秒
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.TAG_NAME, "table"))
)

# 提取页面渲染后的HTML,交给pandas读取
page_source = driver.page_source
tables = pd.read_html(page_source)
# 处理表头
df = tables[0]
df.columns = df.iloc[0]
df = df[1:]

# 关闭浏览器
driver.quit()

print(df.head())

解决方案2:直接调用后台数据接口(效率更高,无需启动浏览器)

该站点的筛选数据通过独立的后端接口返回,你可以直接请求接口获取JSON格式数据,直接转换为DataFrame即可,无需解析HTML表格。

  • 代码示例:
import requests
import pandas as pd

header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36',
    'Content-Type': 'application/json'
}
# 筛选条件可根据需求调整参数
payload = {
    "page": 1,
    "pageSize": 1000,
    "sortBy": "symbol",
    "sortOrder": "asc",
    "filters": {}
}

response = requests.post(
    'https://api.otcmarkets.com/v1/screener',
    headers=header,
    json=payload
)
data = response.json()
# 直接将数据列表转换为DataFrame
df = pd.DataFrame(data['records'])
print(df.head())

内容的提问来源于stack exchange,提问作者Andy Yan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 19:09:03