Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IT里的交易员
Python_edgetts
提交
7e6885ab
P
Python_edgetts
项目概览
IT里的交易员
/
Python_edgetts
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python_edgetts
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
7e6885ab
编写于
5月 07, 2023
作者:
6
62ddd5581cad725575cc5387
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Sun May 7 11:25:00 UTC 2023 inscode
上级
3d0d5c63
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
129 addition
and
95 deletion
+129
-95
main.py
main.py
+129
-95
未找到文件。
main.py
浏览文件 @
7e6885ab
print
(
'欢迎来到 InsCode'
)
# -*- coding: utf-8 -*-
'''
通达信-小达
使用浏览器登录https://wenda.tdx.com.cn/,并按F12查看网络,复制cookie,替换后即可使用。
'''
import
prettytable
as
pt
# ===============表格美化输出===============
def
df_table
(
df
,
index
):
#利用prettytable对输出结果进行美化,index为索引列名:df_table(df,'market')
tb
=
pt
.
PrettyTable
()
# 如果为trade_time为index转换为日期类型,其它不用管。
if
index
==
"trade_time"
:
df
=
df
.
set_index
(
index
)
df
.
index
=
pd
.
DatetimeIndex
(
df
.
index
)
# df.reset_index(level=None, drop=True, inplace=True, col_level=0, col_fill='')
df
=
df
.
reset_index
(
drop
=
True
)
tb
.
add_column
(
index
,
df
.
index
)
#按date排序
for
col
in
df
.
columns
.
values
:
#df.columns.values的意思是获取列的名称
# print('col',col)
# print('df[col]',df[col])
tb
.
add_column
(
col
,
df
[
col
])
print
(
tb
)
import
requests
import
browser_cookie3
import
pandas
as
pd
import
json
,
os
# 增加UserAgent,防止被反爬虫拦截
from
fake_useragent
import
UserAgent
import
json
,
os
,
random
class
TDX_xiaoda
:
def
__init__
(
self
,
cookie
=
'Hm_lvt_5c4c948b141e4d66943a8430c3d600d0=1683343445; Hm_lpvt_5c4c948b141e4d66943a8430c3d600d0=1683343445; Token=a83cb29c37c54675883f20e7f7e474e7_1_JX_2; TDXID=R2302141428276685425IEYT; ATYPE=999; BTYPE=300; NICK=; LST=11; LOGID=@ec0:IZTanHlrWliPVC3nbJ+DhUKrc5uZJHC5; ASPSessionID=3683314944918358529'
):
self
.
cookie
=
cookie
def
_get_ua
(
self
):
# 构造useragent,为避免网络访问时延及报错,fake_useragent.json存放到本地
location
=
os
.
path
.
dirname
(
__file__
)
+
'/fake_useragent.json'
ua
=
UserAgent
(
cache_path
=
location
)
return
ua
.
random
'''
通达信-小达,使用chrome浏览器登录后,此工具可以自动获取ASPSessionID,并实现访问。
'''
def
__init__
(
self
,
cookie
=
''
):
# 如果类传送Cookie则使用传参
if
cookie
==
''
:
self
.
cookie
=
self
.
get_cookie_tdx
()
else
:
self
.
cookie
=
cookie
def
set_headers
(
self
):
self
.
headers
=
{
# 增加UserAgent,防止被反爬虫拦截
user_agent_list
=
[
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
,
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
,
"Mozilla/5.0 (Windows NT 10.0; WOW64) Gecko/20100101 Firefox/61.0"
,
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
,
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36"
,
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
,
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
,
"Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"
,
]
headers
=
{
'Cookie'
:
self
.
cookie
,
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.70'
'User-Agent'
:
self
.
_get_ua
()
}
return
self
.
headers
'User-Agent'
:
random
.
choice
(
user_agent_list
)}
return
headers
def
get_cookie_tdx
(
self
):
# 查找指定域名的制定Cookie值
cookies
=
browser_cookie3
.
chrome
(
domain_name
=
'wenda.tdx.com.cn'
)
for
item
in
cookies
:
if
item
.
name
==
"ASPSessionID"
:
# print('%s = %s' % (item.name, item.value))
cookie_tdx
=
'ASPSessionID='
+
item
.
value
+
';'
return
cookie_tdx
def
get_data_option
(
self
,
word
=
'涨停股票'
):
'''
获取提示参考
:param word:
:return:
'''
data
=
[{
"op_flag"
:
1
,
"question"
:
word
,
"POS"
:
0
,
"COUNT"
:
10
,
"RANG"
:
"AG"
}]
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.QuestionImagine&RI='
headers
=
self
.
set_headers
()
res
=
requests
.
post
(
url
=
url
,
data
=
json
.
dumps
(
data
),
headers
=
headers
)
text
=
res
.
json
()
return
text
data
=
[{
"op_flag"
:
1
,
"question"
:
word
,
"POS"
:
0
,
"COUNT"
:
10
,
"RANG"
:
"AG"
}]
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.QuestionImagine&RI='
headers
=
self
.
set_headers
()
res
=
requests
.
post
(
url
=
url
,
data
=
json
.
dumps
(
data
),
headers
=
headers
)
res_json
=
res
.
json
()
return
res_json
def
get_word_code
(
self
,
name
=
'换手率2.8~15,量比2~20,OBV指标创新高,涨幅2~7,股价2~30'
):
'''
:param name:
:return:
'''
data
=
[{
"message"
:
name
,
"TDXID"
:
""
,
"wdbk"
:
""
,
"RANG"
:
"AG"
}]
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.StockSelect&RI='
headers
=
self
.
set_headers
()
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
text
=
res
.
json
()
print
(
text
)
code
=
text
[
-
1
][
0
]
data
=
[{
"message"
:
name
,
"TDXID"
:
""
,
"wdbk"
:
""
,
"RANG"
:
"AG"
}]
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.StockSelect&RI='
headers
=
self
.
set_headers
()
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
res_json
=
res
.
json
()
code
=
res_json
[
-
1
][
0
]
return
code
def
get_all_option_data
(
self
,):
def
get_all_option_data
(
self
):
'''
获取全部参考
:return:
'''
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.SmartQuery&RI='
headers
=
self
.
set_headers
()
data
=
[{
"op_flag"
:
1
,
"order_field"
:
""
,
"order_flag"
:
1
,
"cond_json"
:
""
,
"POS"
:
0
,
"COUNT"
:
-
1
,
"RANG"
:
"AG"
}]
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
text
=
res
.
json
()
df
=
pd
.
DataFrame
(
text
)
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.SmartQuery&RI='
headers
=
self
.
set_headers
()
data
=
[{
"op_flag"
:
1
,
"order_field"
:
""
,
"order_flag"
:
1
,
"cond_json"
:
""
,
"POS"
:
0
,
"COUNT"
:
-
1
,
"RANG"
:
"AG"
}]
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
res_json
=
res
.
json
()
df
=
pd
.
DataFrame
(
res_json
)
df
=
df
.
iloc
[
1
:]
df2
=
df
.
rename
(
columns
=
df
.
iloc
[
0
])
df3
=
df2
.
iloc
[
1
:]
return
df3
def
get_word_result
(
self
,
word
=
'北向资金'
):
def
get_word_result
(
self
,
word
=
'今日涨停'
,
try_times
=
30
):
'''
获取数据
:param word:
:return:
根据关键字分析后
获取数据
:param word:
关键字
:return:
字典,包含状态和数据
'''
while
True
:
while
try_times
>
0
:
url
=
'https://wenda.tdx.com.cn/TQL?Entry=NLPSE.NLPQuery&RI=6BFD'
code
=
self
.
get_word_code
(
name
=
word
)
headers
=
self
.
set_headers
()
data
=
[{
"nlpse_id"
:
code
,
"POS"
:
0
,
"COUNT"
:
100000
,
"order_field"
:
""
,
"dynamic_order"
:
""
,
"order_flag"
:
""
,
"timestamps"
:
0
,
"op_flag"
:
1
,
"screen_type"
:
1
,
"RANG"
:
"AG"
}]
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
text
=
res
.
content
.
decode
(
'utf-8'
)
text
=
json
.
loads
(
text
)
data_len
=
len
(
text
)
if
data_len
<
4
:
print
(
'
连接失败,再次尝试连接*********'
+
str
(
text
)
)
headers
=
self
.
set_headers
()
data
=
[{
"nlpse_id"
:
code
,
"POS"
:
0
,
"COUNT"
:
100000
,
"order_field"
:
""
,
"dynamic_order"
:
""
,
"order_flag"
:
""
,
"timestamps"
:
0
,
"op_flag"
:
1
,
"screen_type"
:
1
,
"RANG"
:
"AG"
}]
res
=
requests
.
post
(
url
=
url
,
headers
=
headers
,
data
=
json
.
dumps
(
data
))
text
=
res
.
content
.
decode
(
'utf-8'
)
res_json
=
json
.
loads
(
text
)
if
len
(
res_json
[
1
])
<
4
:
try_times
-=
1
print
(
'
获取信息失败,剩余'
+
str
(
try_times
)
+
'次尝试机会,再次尝试连接......'
)
else
:
print
(
text
)
df
=
pd
.
DataFrame
(
text
)
break
df
=
df
.
iloc
[
1
:]
df2
=
df
.
rename
(
columns
=
df
.
iloc
[
0
])
df3
=
df2
.
iloc
[
2
:]
return
df3
if
__name__
==
'__main__'
:
# print(text)
df
=
pd
.
DataFrame
(
res_json
)
df
=
df
.
iloc
[
1
:]
df2
=
df
.
rename
(
columns
=
df
.
iloc
[
0
])
df3
=
df2
.
iloc
[
2
:]
return
{
"success"
:
True
,
"df_data"
:
df3
.
round
(
3
)
}
return
{
"success"
:
False
,
"msg"
:
'获取行情信息结果为None'
}
#获取参考
xd
=
TDX_xiaoda
()
# ===============表格美化输出===============
def
df_table
(
df
,
index
):
import
prettytable
as
pt
#利用prettytable对输出结果进行美化,index为索引列名:df_table(df,'market')
tb
=
pt
.
PrettyTable
()
# 如果为trade_time为index转换为日期类型,其它不用管。
if
index
==
"trade_time"
:
df
=
df
.
set_index
(
index
)
df
.
index
=
pd
.
DatetimeIndex
(
df
.
index
)
# df.reset_index(level=None, drop=True, inplace=True, col_level=0, col_fill='')
df
=
df
.
reset_index
(
drop
=
True
)
tb
.
add_column
(
index
,
df
.
index
)
#按date排序
for
col
in
df
.
columns
.
values
:
#df.columns.values的意思是获取列的名称
# print('col',col)
# print('df[col]',df[col])
tb
.
add_column
(
col
,
df
[
col
])
print
(
tb
)
if
__name__
==
'__main__'
:
# 以下两种方法均可以,推荐方法2。注册请自行搞定。
# 方法1:使用任意浏览器登录https://wenda.tdx.com.cn/,并按F12查看网络,找到Cookie复制后替换这里ASPSessionID后数值即可使用。
# xd = TDX_xiaoda(cookie = 'ASPSessionID=1881848563923616200')
# 方法2:使用Chrome浏览器登录https://wenda.tdx.com.cn/后,无需手工查找Cookie,程序会自动获取ASPSessionID,并实现访问。但必须使用Chrome浏览器。
xd
=
TDX_xiaoda
()
if
0
:
#可能获取不到
df1
=
xd
.
get_data_option
(
word
=
'沪深A股,换手率1%~5%'
)
print
(
df1
)
# [[0, '', 10, '', ''], ['POS', 'question'], [], ['1', '沪深A股,换手率5%~10%'], ['2', '换手率1%~5%'], ['3', '换手率1%~5%'], ['4', '沪深A股'], ['5', '沪深A股'], ['6', '沪深A股,电力'], ['7', '沪深A股,化工'], ['8', '换手率1%~5%,量比1~3'], ['9', '换手率5%~10%'], ['10', '换手率大于5%']]
ret
=
xd
.
get_data_option
(
word
=
'沪深A股,换手率1%~5%'
)
print
(
ret
)
#获取问题转代码
df2
=
xd
.
get_word_code
(
name
=
'沪深A股,换手率1%~5%'
)
print
(
df2
)
ret
=
xd
.
get_word_code
(
name
=
'沪深A股,换手率1%~5%'
)
print
(
ret
)
#获取全部的参考,具体看通达信
df3
=
xd
.
get_all_option_data
()
print
(
df3
)
df_table
(
df3
,
'df'
)
ret
=
xd
.
get_all_option_data
()
# print(ret)
df_table
(
ret
,
'df'
)
if
1
:
#获取问题的结果
words
=
'沪深A股,周线MACD持续向上,今日涨停'
# 连板 3连板 今日涨停 20230201涨停 北向资金 板块 大单抢筹 MACD背离
ret
=
xd
.
get_word_result
(
word
=
words
)
if
ret
[
'success'
]:
df
=
ret
[
'df_data'
]
# print(df)
df_table
(
df
,
'df'
)
filename
=
os
.
path
.
dirname
(
__file__
)
+
'
\\
'
+
'通达信-小达选股_'
+
words
+
'.xlsx'
df
.
to_excel
(
filename
)
print
(
'选股完成,结果写入:'
,
filename
)
#获取问题的结果
df4
=
xd
.
get_word_result
(
word
=
'今日涨停'
)
print
(
df4
)
df_table
(
df4
,
'df'
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录