query table
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.
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.
实盘时,如果某网站宕机或改版,可以立即切换到其他网站。(注意:不同网站的表格结构不同,需要提前做适配)
RooCode
提供了Human Reply
功能。但发现纳米搜索
网页版复制时格式破坏,所以研发了此功能
pip install -i https://pypi.org/simple --upgrade mcp_query_table
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade mcp_query_table
import asyncio
from mcp_query_table import *
async def main() -> None:
async with BrowserManager(cdp_endpoint="http://127.0.0.1:9222", executable_path=None, debug=true) as bm:
# 问财需要保证浏览器宽度>768,防止界面变成适应手机
page = await bm.get_page()
df = await query(page, '收益最好的200只ETF', query_type=QueryType.ETF, max_page=1, site=Site.THS)
print(df.to_markdown())
df = await query(page, '年初至今收益率前50', query_type=QueryType.Fund, max_page=1, site=Site.TDX)
print(df.to_csv())
df = await query(page, '流通市值前10的行业板块', query_type=QueryType.Index, max_page=1, site=Site.TDX)
print(df.to_csv())
# TODO 东财翻页要提前登录
df = await query(page, '今日涨幅前5的概念板块;', query_type=QueryType.Board, max_page=3, site=Site.EastMoney)
print(df)
output = await chat(page, "1+2等于多少?", provider=Provider.YuanBao)
print(output)
output = await chat(page, "3+4等于多少?", provider=Provider.YuanBao, create=true)
print(output)
print('done')
bm.release_page(page)
await page.wait_for_timeout(2000)
if __name__ == '__main__':
asyncio.run(main())
Chrome
。如一定要使用Edge
,除了关闭Edge
所有窗口外,还要在任务管理器关闭Microsoft Edge
的所有进程,即taskkill /f /im msedge.exe
不同于requests
,playwright
是基于浏览器的,模拟用户在浏览器中的操作。
requests
,但开发效率高数据的获取有:
json
数据requests
,需要做响应解析此项目采用的是模拟点击浏览器来发送请求,使用截获响应并解析的方法来获取数据。
后期会根据不同的网站改版情况,使用更适合的方法。
确保可以在控制台中执行python -m mcp_query_table -h
。如果不能,可能要先pip install mcp_query_table
在Cline
中可以配置如下。其中command
是python
的绝对路径,executable_path
是Chrome
的绝对路径,timeout
是超时时间,单位为秒。
在各AI
平台中由于返回时间常需1分钟以上,所以需要设置大的超时时间。
{
"mcpServers": {
"mcp_query_table": {
"timeout": 300,
"command": "D:UsersKanminiconda3envspy312python.exe",
"args": [
"-m",
"mcp_query_table",
"--format",
"markdown",
"--cdp_endpoint",
"http://127.0.0.1:9222",
"--executable_path",
"C:Program FilesGoogleChromeApplicationchrome.exe"
]
}
}
}
先在控制台中执行如下命令,启动MCP
服务
python -m mcp_query_table --format markdown --transport sse --port 8000
然后就可以连接到MCP
服务了
{
"mcpServers": {
"mcp_query_table": {
"timeout": 300,
"url": "http://127.0.0.1:8000/sse"
}
}
}
MCP Inspector
进行调试npx @modelcontextprotocol/inspector python -m mcp_query_table --format markdown
打开浏览器并翻页是一个比较耗时的操作,会导致MCP Inspector
页面超时,可以http://localhost:5173/?timeout=300000
表示超时时间为300秒
第一次尝试编写MCP
项目,可能会有各种问题,欢迎大家交流。
MCP
使用技巧2024年涨幅最大的100只股票按2024年12月31日总市值排名。三个网站的结果都不一样
大语言模型对问题拆分能力弱,所以要能合理的提问,保证查询条件不会被改动。以下推荐第2、3种
大语言模型非常有可能拆分这句,导致一步查询被分成了多步查询
用引号括起来,避免被拆分
分成两步查询,先查询板块,再查询股票。但最好不要全自动,因为第一步的结果它不理解“今日涨幅”和“区间涨幅”,需要交互修正