Supports output formatting in Markdown format, useful for displaying tabular data in a readable format
Provides installation source for the package through PyPI's package repository
Functions as a Python package for querying stock information tables from financial websites
Referenced as part of the web scraping methodology, though the tool primarily uses Playwright instead
mcp_query_table
- 基于
playwright
实现的财经网页表格爬虫,支持Model Context Protocol (MCP)
。目前可查询来源为实盘时,如果某网站宕机或改版,可以立即切换到其他网站。(注意:不同网站的表格结构不同,需要提前做适配)
- 基于
playwright
实现的大语言模型调用爬虫。目前可用来源为RooCode
提供了Human Reply
功能。但发现纳米搜索
网页版复制时格式破坏,所以研发了此功能
安装
使用
注意事项
- 浏览器最好是
Chrome
。如一定要使用Edge
,除了关闭Edge
所有窗口外,还要在任务管理器关闭Microsoft Edge
的所有进程,即taskkill /f /im msedge.exe
- 浏览器要保证窗口宽度,防止部分网站自动适配成手机版,导致表格查询失败
- 如有网站账号,请提前登录。此工具无自动登录功能
- 不同网站的表格结构不同,同条件返回股票数量也不同。需要查询后做适配
工作原理
不同于requests
,playwright
是基于浏览器的,模拟用户在浏览器中的操作。
- 不需要解决登录问题
- 不需要解决请求构造、响应解析
- 可以直接获取表格数据,所见即所得
- 运行速度慢于
requests
,但开发效率高
数据的获取有:
- 直接解析HTML表格
- 数字文本化了,不利于后期研究
- 适用性最强
- 截获请求,获取返回的
json
数据- 类似于
requests
,需要做响应解析 - 灵活性差点,网站改版后,需要重新做适配
- 类似于
此项目采用的是模拟点击浏览器来发送请求,使用截获响应并解析的方法来获取数据。
后期会根据不同的网站改版情况,使用更适合的方法。
无头模式
无头模式运行速度更快,但部分网站需要提前登录,所以,无头模式一定要指定user_data_dir
,否则会出现需要登录的情况。
endpoint=None
时,headless=True
可无头启动新浏览器实例。指定executable_path
和user_data_dir
,才能确保无头模式下正常运行。endpoint
以http://
开头,连接CDP
模式启动的有头浏览器,参数必有--remote-debugging-port
。executable_path
为本地浏览器路径。endpoint
以ws://
开头,连接远程Playwright Server
。也是无头模式,但无法指定user_data_dir
,所以使用受限
MCP支持
确保可以在控制台中执行python -m mcp_query_table -h
。如果不能,可能要先pip install mcp_query_table
在Cline
中可以配置如下。其中command
是python
的绝对路径,timeout
是超时时间,单位为秒。 在各AI
平台中由于返回时间常需1分钟以上,所以需要设置大的超时时间。
STDIO方式
SSE方式
先在控制台中执行如下命令,启动MCP
服务
然后就可以连接到MCP
服务了
使用MCP Inspector
进行调试
打开浏览器并翻页是一个比较耗时的操作,会导致MCP Inspector
页面超时,可以http://localhost:5173/?timeout=300000
表示超时时间为300秒
第一次尝试编写MCP
项目,可能会有各种问题,欢迎大家交流。
MCP
使用技巧
- 2024年涨幅最大的100只股票按2024年12月31日总市值排名。三个网站的结果都不一样
- 同花顺:显示了2201只股票。前5个是工商银行、农业银行、中国移动、中国石油、建设银行
- 通达信:显示了100只股票,前5个是寒武纪、正丹股份,汇金科技、万丰奥威、艾融软件
- 东方财富:显示了100只股票,前5个是海光信息、寒武纪、光启技术、润泽科技、新易盛
- 大语言模型对问题拆分能力弱,所以要能合理的提问,保证查询条件不会被改动。以下推荐第2、3种
- 2024年涨幅最大的100只股票按2024年12月31日总市值排名
大语言模型非常有可能拆分这句,导致一步查询被分成了多步查询
- 向东方财富查询“2024年涨幅最大的100只股票按2024年12月31日总市值排名”
用引号括起来,避免被拆分
- 向东方财富板块查询 “去年涨的最差的行业板块”,再查询此板块中去年涨的最好的5只股票
分成两步查询,先查询板块,再查询股票。但最好不要全自动,因为第一步的结果它不理解“今日涨幅”和“区间涨幅”,需要交互修正
- 2024年涨幅最大的100只股票按2024年12月31日总市值排名
支持Streamlit
实现在同一页面中查询金融数据,并手工输入到AI
中进行深度分析。参考streamlit
目录下的README.md
文件。
参考
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
A web scraper for financial table data that implements the Model Context Protocol, allowing users to query stock data from multiple Chinese financial websites including THS, TDX, and EastMoney.
Related Resources
Related MCP Servers
- -securityFlicense-qualityA Model Context Protocol server that enables Claude Desktop to access and analyze Chinese and global financial market data through the AKShare library.Last updated -3Python
- AsecurityAlicenseAqualityA Model Context Protocol server that enables interaction with Yahoo Finance to retrieve stock pricing, company information, and historical financial data through natural language queries.Last updated -92PythonMIT License
- -securityFlicense-qualityProvides tools to get financial data (stock prices, company information) and generate financial visualizations through the Model Context Protocol.Last updated -2Python
- AsecurityAlicenseAqualityA Model Context Protocol server providing tools for querying A-share stock market data, including historical prices, financial reports, market indices, and macroeconomic indicators.Last updated -28178PythonMIT License