雪球模拟组合收益率爬取

实现的功能:

1)爬取雪球组合六个月以内,每天的累积收益率值(时间比六个月长就不给提供每天的累积收益率了);
2)每天收盘后爬取当天的累积收益率;
3)延伸 1:把上边两点加起来就能一直跟踪组合的收益率了;
4)延伸 2:设置定时脚本,写程序定时执行上边的三个步骤,就可以每天只看一眼,时间变得简单一点点;

代码

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
import requests,json

# 获取六个月内的每天累计收益率
def get_return_rate_six_month(gid, cookie):
headers = {
'authority': 'tc.xueqiu.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
'sec-ch-ua-platform': '"Windows"',
'origin': 'https://xueqiu.com',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://xueqiu.com/performance',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'cookie': cookie,
}

params = (
('gid', gid),
('period', '6m'), # 每次取最大就行, 时间再长不给返回每天的累计收益率...
('market', 'ALL'),
)

response = requests.get('https://tc.xueqiu.com/tc/snowx/MONI/forchart/roa.json', headers=headers, params=params)
content = json.loads(response.text)
return content['result_data']['list']

res= pd.DataFrame()
gid = 'your gid'
cookie = 'your cookies'
data = get_return_rate(gid)
for element in data:
value_dict = dict()
value_dict['return_rate'] = element['value']
value_dict['date'] = element['date']
res= ers.append(value_dict, ignore_index = True)
res.head()


# 获取每天的累积收益率
def get_accum_rate_each_day(gid, cookie):
headers = {
'authority': 'tc.xueqiu.com',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'accept': 'application/json, text/plain, */*',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
'sec-ch-ua-platform': '"Windows"',
'origin': 'https://xueqiu.com',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty',
'referer': 'https://xueqiu.com/performance',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7',
'cookie': cookie,
}

params = (
('gid', gid),
)

response = requests.get('https://tc.xueqiu.com/tc/snowx//MONI/performances.json', headers=headers, params=params)

response.encoding = "utf-8"
content = json.loads(response.text)
if content['msg'] == '请求超时,请检查网络并重试':
print("请求错误...需要更新 cookies... ")
return

accum_rate = content['result_data']['performances'][1]['accum_rate']
return accum_rate

value = get_accum_rate_each_day(gid, cookie)

补充阅读

windows 定时脚本设置
https://www.cnblogs.com/sui776265233/p/13602893.html
linux 定时脚本设置
https://www.jianshu.com/p/d93e2b177814

(✪ω✪)