Sun May 7 02:21:00 UTC 2023 inscode

上级 becd727a
print('欢迎来到 InsCode')
\ No newline at end of file
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 pandas as pd
import json,os
# 增加UserAgent,防止被反爬虫拦截
from fake_useragent import UserAgent
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
def set_headers(self):
self.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
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
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]
return code
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)
df = df.iloc[1:]
df2 = df.rename(columns=df.iloc[0])
df3 = df2.iloc[1:]
return df3
def get_word_result(self,word='北向资金'):
'''
获取数据
:param word:
:return:
'''
while True:
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))
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__':
#获取参考
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%']]
#获取问题转代码
df2=xd.get_word_code(name='沪深A股,换手率1%~5%')
print(df2)
#获取全部的参考,具体看通达信
df3=xd.get_all_option_data()
print(df3)
df_table(df3,'df')
#获取问题的结果
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.
先完成此消息的编辑!
想要评论请 注册