Query Table

Query Table

query table from some websites, support MCP. Contribute to wukan1986/query_table development by creating an account on GitHub.

wukan1986

Media Content Processing
Visit Server

README

query_table

基于playwright实现的网页表格爬虫,支持Model Context Protocol (MCP) 。目前可查询来源为

实盘时,如果某网站宕机或改版,可以立即切换到其他网站。(注意:不同网站的表格结构不同,需要提前做适配)

安装

pip install -i https://pypi.org/simple --upgrade query_table
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade query_table

使用

import asyncio

from query_table import *


async def main() -> None:
    # 启动浏览器,browser_path最好是Chrome的绝对路径
    playwright, browser, context, page = await launch_browser(port=9222, browser_path=None)
    print(browser.is_connected(), page.is_closed())

    # # 问财需要保证浏览器宽度>768,防止界面变成适应手机
    df = await query(page, '上证50成分股', query_type=QueryType.CNStock, max_page=3, site=Site.THS)
    print(df.to_markdown())
    df = await query(page, '收盘价>50元', query_type=QueryType.CNStock, max_page=3, site=Site.TDX)
    print(df.to_csv())
    # # TODO 东财翻页要提前登录
    df = await query(page, '收盘价>50元', query_type=QueryType.CNStock, max_page=3, site=Site.EastMoney)
    print(df)

    print('done')
    await browser.close()
    await playwright.stop()


if __name__ == '__main__':
    asyncio.run(main())

注意事项

  1. 浏览器最好是Chrome。如一定要使用Edge,除了关闭Edge所有窗口外,还要在任务管理器关闭Microsoft Edge 的所有进程,即taskkill /f /im msedge.exe
  2. 浏览器要保证窗口宽度,防止部分网站自动适配成手机版,导致表格查询失败
  3. 如有网站账号,请提前登录。此工具无自动登录功能
  4. 不同网站的表格结构不同,同条件返回股票数量也不同。需要查询后做适配

工作原理

不同于requestsplaywright是基于浏览器的,模拟用户在浏览器中的操作。

  1. 不需要解决登录问题
  2. 不需要解决请求构造、响应解析
  3. 可以直接获取表格数据,所见即所得
  4. 运行速度慢于requests,但开发效率高

数据的获取有:

  1. 直接解析HTML表格
    1. 数字文本化了,不利于后期研究
    2. 适用性最强
  2. 截获请求,获取返回的json数据
    1. 类似于requests,需要做响应解析
    2. 灵活性差点,网站改版后,需要重新做适配

此项目采用的是模拟点击浏览器来发送请求,使用截获响应并解析的方法来获取数据。

后期会根据不同的网站改版情况,使用更适合的方法。

MCP支持

确保可以在控制台中执行python -m query_table -h。如果不能,可能要先pip install query_table

Cline中可以配置如下。其中commandpython的绝对路径,browser_pathChrome的绝对路径。

{
  "mcpServers": {
    "query_table": {
      "command": "D:\\Users\\Kan\\miniconda3\\envs\\py312\\python.exe",
      "args": [
        "-m",
        "query_table",
        "--format",
        "markdown",
        "--browser_path",
        "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
      ]
    }
  }
}

使用MCP Inspector进行调试

npx @modelcontextprotocol/inspector python -m query_table --format markdown

第一次尝试编写MCP项目,可能会有各种问题,欢迎大家交流。

MCP使用技巧

  1. 2024年涨幅最大的100只股票按2024年12月31日总市值排名。三个网站的结果都不一样

    • 同花顺:显示了2201只股票。前5个是工商银行、农业银行、中国移动、中国石油、建设银行
    • 通达信:显示了100只股票,前5个是寒武纪、正丹股份,汇金科技、万丰奥威、艾融软件
    • 东方财富:显示了100只股票,前5个是海光信息、寒武纪、光启技术、润泽科技、新易盛
  2. 大语言模型对问题拆分能力弱,所以要能合理的提问,保证查询条件不会被改动。以下推荐第二种

    • 2024年涨幅最大的100只股票按2024年12月31日总市值排名
    • 向东方财富查询“2024年涨幅最大的100只股票按2024年12月31日总市值排名”

参考

Recommended Servers

Mult Fetch MCP Server

Mult Fetch MCP Server

A versatile MCP-compliant web content fetching tool that supports multiple modes (browser/node), formats (HTML/JSON/Markdown/Text), and intelligent proxy detection, with bilingual interface (English/Chinese).

Featured
Local
Hyperbrowser MCP Server

Hyperbrowser MCP Server

Welcome to Hyperbrowser, the Internet for AI. Hyperbrowser is the next-generation platform empowering AI agents and enabling effortless, scalable browser automation. Built specifically for AI developers, it eliminates the headaches of local infrastructure and performance bottlenecks, allowing you to

Featured
Local
Web Research Server

Web Research Server

A Model Context Protocol server that enables Claude to perform web research by integrating Google search, extracting webpage content, and capturing screenshots.

Featured
Youtube Translate

Youtube Translate

A Model Context Protocol server that enables access to YouTube video content through transcripts, translations, summaries, and subtitle generation in various languages.

Featured
Fetch MCP Server

Fetch MCP Server

Provides functionality to fetch web content in various formats, including HTML, JSON, plain text, and Markdown.

Featured
Web Research Server

Web Research Server

MCP web research server (give Claude real-time info from the web) - oneshot-engineering/mcp-webresearch

Featured
Jina AI

Jina AI

Contribute to JoeBuildsStuff/mcp-jina-ai development by creating an account on GitHub.

Featured
Workflow Learner

Workflow Learner

To teach LLM Agents understand workflows with PSR.exe's hmt.

Local
Chat Summary Server

Chat Summary Server

summarize chat message

Local
DEEP Web Research

DEEP Web Research

The MCP Web Research Server enables real-time web research with Claude by integrating Google search, capturing webpage content and screenshots, and tracking research sessions.

Local