python 同花顺和雪球同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import re
import time
from pprint import pprint
  
import pandas as pd
import requests
  
  
class Snowball:
    xq = 'https://xueqiu.com'
    # 雪球自选股列表相关json
    url = {'get': xq + '/v4/stock/portfolio/stocks.json',
           'del': xq + '/stock/portfolio/delstock.json',
           'add': xq + '/v4/stock/portfolio/addstock.json',
           'modify': xq + '/v4/stock/portfolio/modifystocks.json'}
    # 默认cookie
    df_cookie = ('s=××××××; '
                 'xq_a_token=×××××××××××××××××××××; '
                 'xq_r_token=×××××××××××××××××××××; '
                 )
  
    def __init__(self, uid, cookie=df_cookie):
        self.uid = uid  # 用户页面如 https://xueqiu.com/×××××××××
        self.cookie = cookie  # 操作列表需要该用户登录的cookie
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) '
                                      'Gecko/20100101 Firefox/56.0',
                        'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                        'Accept-Encoding': 'gzip, deflate, br',
                        'Referer': Snowball.xq + '/' + uid,
                        'Cookie': cookie,
                        'DNT': '1'}
        self.stocks = pd.DataFrame()  # 雪球自选股清单
  
    def get_stocks(self):
        # 获取雪球自选股列表
        try:
            payload = {'size': 1000, 'tuid': self.uid, 'uid': self.uid, 'pid': -1, 'category': 2, 'type': 1}
            response = requests.get(Snowball.url['get'], params=payload, headers=self.headers, timeout=10)
            # pprint(response.content)
            self.stocks = pd.DataFrame(response.json()['stocks'])
        except Exception, e:
            print 'get_stocks @', self.uid, '; error:', e
            pprint(payload)
            pprint(self.headers)
            return False
        else:
            # pprint(self.stocks)
            return self.stocks
  
    def del_stock(self, code):
        # 在雪球删除指定代码的股票
        try:
            payload = {'code': code}
            response = requests.post(Snowball.url['del'], data=payload, headers=self.headers, timeout=10)
            # pprint(response.content)
            response = response.json()['success']
            if response == True:
                print 'del_stock', code, 'success.'
            else:
                print 'del_stock', code, 'failed.'
        except Exception, e:
            print 'del_stock', code, '@', self.uid, '; error:', e
            pprint(payload)
            pprint(self.headers)
            return False
        else:
            self.get_stocks()
            return response
  
    def add_stock(self, code):
        # 在雪球添加指定代码的股票
        try:
            payload = {'symbol': code, 'category': 2, 'isnotice': 1}
            response = requests.post(Snowball.url['add'], data=payload, headers=self.headers, timeout=10)
            # pprint(response.content)
            response = response.json()['success']
            if response == True:
                print 'add_stock', code, 'success.'
            else:
                print 'add_stock', code, 'failed.'
        except Exception, e:
            print 'add_stock', code, '@', self.uid, '; error:', e
            pprint(payload)
            pprint(self.headers)
            return False
        else:
            self.get_stocks()
            return response
  
    def modify_stocks(self, code_list=[]):
        # 雪球自选股列表排序
        try:
            payload = {'pid': -1, 'type': 1, 'stocks': ','.join(code_list)}
            response = requests.post(Snowball.url['modify'], data=payload, headers=self.headers, timeout=10)
            # pprint(response.content)
            response = response.json()['success']
            if response == True:
                print 'modify_stocks', code_list, 'success.'
            else:
                print 'modify_stocks', code_list, 'failed.'
        except Exception, e:
            print 'modify_stocks', code_list, '@', self.uid, '; error:', e
            pprint(payload)
            pprint(self.headers)
            return False
        else:
            self.get_stocks()
            return response
  
  
class Tonghuashun:
    # 同花顺自选股列表相关
    url = {'get': 'http://pop.10jqka.com.cn/getselfstockinfo.php',
           'modify': 'http://stock.10jqka.com.cn/self.php'}
  
    def __init__(self, uid, cookie):
        self.uid = uid
        self.cookie = cookie  # 该用户登录的cookie
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) '
                                      'Gecko/20100101 Firefox/56.0',
                        'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                        'Accept-Encoding': 'gzip, deflate',
                        'Referer': 'http://stock.10jqka.com.cn/my/zixuan.shtml',
                        'Cookie': cookie,
                        'DNT': '1'}
        self.stocks = pd.DataFrame()  # 同花顺自选股清单
  
    def get_stocks(self):
        # 获取同花顺自选股列表
        try:
            payload = {'callback': 'callback' + str(int(time.time() * 1000))}
            response = requests.get(Tonghuashun.url['get'], params=payload, headers=self.headers, timeout=10)
            # pprint(response.content)
            self.stocks = pd.DataFrame(response.json())
        except Exception, e:
            print 'get_stocks @', self.uid, '; error:', e
            pprint(payload)
            pprint(self.headers)
            return False
        else:
            # pprint(self.stocks)
            return self.stocks
  
    def modify_stock(self, code, method, pos='1'):
        # 更改同花顺自选股列表
        # method: add 添加, del 删除, exc 排序
        # pos: 排序用的序号, 从1开始
        try:
            payload = {'add': {'stockcode': code, 'op': 'add'},
                       'del': {'stockcode': code, 'op': 'del'},
                       'exc': {'stockcode': code, 'op': 'exc', 'pos': pos, 'callback': 'callbacknew'}
                       }
            # self.get_stocks()
            response = requests.get(Tonghuashun.url['modify'], params=payload[method], headers=self.headers, timeout=10)
            # pprint(response.content)
            response = response.content.decode('gbk')
            print 'modify_stocks', method, pos, code, response
            if response == u'修改自选股成功':
                response = True
        except Exception, e:
            print 'modify_stock', method, code, '@', self.uid, '; error:', e
            pprint(payload[method])
            pprint(self.headers)
            return False
        else:
            self.get_stocks()
            return response

 

posted @   锐洋智能  阅读(534)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
点击右上角即可分享
微信分享提示