-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgetEastData.py
430 lines (413 loc) · 15.2 KB
/
getEastData.py
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# This Python file uses the following encoding: utf-8
# if __name__ == "__main__":
# pass
import requests
import pandas as pd
import py_mini_racer
from importlib import resources
from bs4 import BeautifulSoup
from tqdm import tqdm
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.27'}
def getStockHot():
url = "https://emappdata.eastmoney.com/stockrank/getAllCurrentList"
payload = {
"appId": "appId01",
"globalId": "786e4c21-70dc-435a-93bb-38",
"marketType": "",
"pageNo": 1,
"pageSize": 100,
}
r = requests.post(url, json=payload,headers=headers)
data_json = r.json()
temp_rank_df = pd.DataFrame(data_json["data"])
temp_rank_df["mark"] = [
"0" + "." + item[2:] if "SZ" in item else "1" + "." + item[2:]
for item in temp_rank_df["sc"]
]
",".join(temp_rank_df["mark"]) + "?v=08926209912590994"
params = {
"ut": "f057cbcbce2a86e2866ab8877db1d059",
"fltt": "2",
"invt": "2",
"fields": "f14,f3,f12,f2",
"secids": ",".join(temp_rank_df["mark"]) + ",?v=08926209912590994",
}
url = "https://push2.eastmoney.com/api/qt/ulist.np/get"
r = requests.get(url, params=params)
data_json = r.json()
temp_df = pd.DataFrame(data_json["data"]["diff"])
temp_df.columns = ["最新价", "涨跌幅", "代码", "股票名称"]
temp_df["当前排名"] = temp_rank_df["rk"]
temp_df["代码"] = temp_rank_df["sc"]
temp_df = temp_df[
[
"当前排名",
"代码",
"股票名称",
"最新价",
"涨跌幅",
]
]
temp_df['最新价'] = pd.to_numeric(temp_df['最新价'], errors="coerce")
temp_df['涨跌幅'] = pd.to_numeric(temp_df['涨跌幅'], errors="coerce")
return temp_df
def stock_hot_tgb():
"""
淘股吧-热门股票
https://www.taoguba.com.cn/stock/moreHotStock
"""
url = "https://www.taoguba.com.cn/stock/moreHotStock"
r = requests.get(url)
temp_df = pd.concat([pd.read_html(r.text, header=0)[0], pd.read_html(r.text, header=0)[1]])
temp_df = temp_df[[
"个股代码",
"个股名称",
]]
temp_df.reset_index(inplace=True, drop=True)
return temp_df
def get_stock_pool_strong(date):
"""
东方财富网-行情中心-涨停板行情-强势股池
http://quote.eastmoney.com/ztb/detail#type=qsgc
"""
url = "http://push2ex.eastmoney.com/getTopicQSPool"
params = {
"ut": "7eea3edcaed734bea9cbfc24409ed989",
"dpt": "wz.ztzt",
"Pageindex": "0",
"pagesize": "170",
"sort": "zdp:desc",
"date": date,
"_": "1621590489736",
}
r = requests.get(url, params=params)
data_json = r.json()
if data_json["data"] is None:
return pd.DataFrame()
temp_df = pd.DataFrame(data_json["data"]["pool"])
temp_df.reset_index(inplace=True)
temp_df["index"] = range(1, len(temp_df) + 1)
temp_df.columns = [
"序号",
"代码",
"_",
"名称",
"最新价",
"涨停价",
"_",
"涨跌幅",
"成交额",
"流通市值",
"总市值",
"换手率",
"是否新高",
"入选理由",
"量比",
"涨速",
"涨停统计",
"所属行业",
]
temp_df["涨停统计"] = (
temp_df["涨停统计"].apply(lambda x: dict(x)["days"]).astype(str)
+ "/"
+ temp_df["涨停统计"].apply(lambda x: dict(x)["ct"]).astype(str)
)
temp_df = temp_df[
[
"序号",
"代码",
"名称",
"涨跌幅",
"最新价",
"涨停价",
"成交额",
"流通市值",
"总市值",
"换手率",
"涨速",
"是否新高",
"量比",
"涨停统计",
"入选理由",
"所属行业",
]
]
temp_df["最新价"] = temp_df["最新价"] / 1000
temp_df["涨停价"] = temp_df["涨停价"] / 1000
return temp_df
def get_stock_hot_keyword(symbol):
"""
东方财富-个股人气榜-关键词
http://guba.eastmoney.com/rank/stock?code=000665
"""
url = "https://emappdata.eastmoney.com/stockrank/getHotStockRankList"
payload = {
"appId": "appId01",
"globalId": "786e4c21-70dc-435a-93bb-38",
"srcSecurityCode": symbol,
}
r = requests.post(url, json=payload)
data_json = r.json()
temp_df = pd.DataFrame(data_json['data'])
del temp_df['flag']
temp_df.columns = ['时间', '股票代码', '概念名称', '概念代码', '热度']
return temp_df
def get_ths_js(file: str = "ths.js"):
"""Get path to data "ths.js" text file.
Returns
-------
pathlib.PosixPath
Path to file.
References
----------
.. [1] E.A.Abbott, ”Flatland”, Seeley & Co., 1884.
"""
with resources.path("akshare.data", file) as f:
data_file_path = f
return data_file_path
def _get_file_content_ths(file: str = "ths.js") -> str:
"""
获取 JS 文件的内容
:param file: JS 文件名
:type file: str
:return: 文件内容
:rtype: str
"""
setting_file_path = get_ths_js(file)
with open(setting_file_path) as f:
file_data = f.read()
return file_data
def stock_fund_flow_concept(symbol):
"""
同花顺-数据中心-资金流向-概念资金流
http://data.10jqka.com.cn/funds/gnzjl/#refCountId=data_55f13c2c_254
"""
js_code = py_mini_racer.MiniRacer()
js_content = _get_file_content_ths("ths.js")
js_code.eval(js_content)
v_code = js_code.call("v")
headers = {
"Accept": "text/html, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"hexin-v": v_code,
"Host": "data.10jqka.com.cn",
"Pragma": "no-cache",
"Referer": "http://data.10jqka.com.cn/funds/gnzjl/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
url = (
"http://data.10jqka.com.cn/funds/gnzjl/field/tradezdf/order/desc/ajax/1/free/1/"
)
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
raw_page = soup.find("span", attrs={"class": "page_info"}).text
page_num = raw_page.split("/")[1]
if symbol == "3日排行":
url = "http://data.10jqka.com.cn/funds/gnzjl/board/3/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
elif symbol == "5日排行":
url = "http://data.10jqka.com.cn/funds/gnzjl/board/5/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
elif symbol == "10日排行":
url = "http://data.10jqka.com.cn/funds/gnzjl/board/10/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
elif symbol == "20日排行":
url = "http://data.10jqka.com.cn/funds/gnzjl/board/20/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
else:
url = "http://data.10jqka.com.cn/funds/gnzjl/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
big_df = pd.DataFrame()
for page in tqdm(range(1, int(page_num) + 1)):
js_code = py_mini_racer.MiniRacer()
js_content = _get_file_content_ths("ths.js")
js_code.eval(js_content)
v_code = js_code.call("v")
headers = {
"Accept": "text/html, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"hexin-v": v_code,
"Host": "data.10jqka.com.cn",
"Pragma": "no-cache",
"Referer": "http://data.10jqka.com.cn/funds/gnzjl/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
r = requests.get(url.format(page), headers=headers)
temp_df = pd.read_html(r.text)[0]
big_df = pd.concat([big_df, temp_df], ignore_index=True)
del big_df["序号"]
big_df.reset_index(inplace=True)
big_df["index"] = range(1, len(big_df) + 1)
if symbol == "即时":
big_df.columns = [
"序号",
"行业",
"行业指数",
"行业-涨跌幅",
"流入资金",
"流出资金",
"净额",
"公司家数",
"领涨股",
"领涨股-涨跌幅",
"当前价",
]
big_df["行业-涨跌幅"] = big_df["行业-涨跌幅"].str.strip("%")
big_df["领涨股-涨跌幅"] = big_df["领涨股-涨跌幅"].str.strip("%")
big_df["行业-涨跌幅"] = pd.to_numeric(big_df["行业-涨跌幅"], errors="coerce")
big_df["领涨股-涨跌幅"] = pd.to_numeric(big_df["领涨股-涨跌幅"], errors="coerce")
else:
big_df.columns = [
"序号",
"行业",
"公司家数",
"行业指数",
"阶段涨跌幅",
"流入资金",
"流出资金",
"净额",
]
return big_df
def stock_fund_flow_industry(symbol):
"""
同花顺-数据中心-资金流向-行业资金流
http://data.10jqka.com.cn/funds/hyzjl/#refCountId=data_55f13c2c_254
:param symbol: choice of {“即时”, "3日排行", "5日排行", "10日排行", "20日排行"}
"""
js_code = py_mini_racer.MiniRacer()
js_content = _get_file_content_ths("ths.js")
js_code.eval(js_content)
v_code = js_code.call("v")
headers = {
"Accept": "text/html, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"hexin-v": v_code,
"Host": "data.10jqka.com.cn",
"Pragma": "no-cache",
"Referer": "http://data.10jqka.com.cn/funds/hyzjl/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
url = (
"http://data.10jqka.com.cn/funds/hyzjl/field/tradezdf/order/desc/ajax/1/free/1/"
)
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, "lxml")
raw_page = soup.find("span", attrs={"class": "page_info"}).text
page_num = raw_page.split("/")[1]
if symbol == "3日排行":
url = "http://data.10jqka.com.cn/funds/hyzjl/board/3/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
elif symbol == "5日排行":
url = "http://data.10jqka.com.cn/funds/hyzjl/board/5/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
elif symbol == "10日排行":
url = "http://data.10jqka.com.cn/funds/hyzjl/board/10/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
elif symbol == "20日排行":
url = "http://data.10jqka.com.cn/funds/hyzjl/board/20/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
else:
url = "http://data.10jqka.com.cn/funds/hyzjl/field/tradezdf/order/desc/page/{}/ajax/1/free/1/"
big_df = pd.DataFrame()
for page in tqdm(range(1, int(page_num) + 1)):
js_code = py_mini_racer.MiniRacer()
js_content = _get_file_content_ths("ths.js")
js_code.eval(js_content)
v_code = js_code.call("v")
headers = {
"Accept": "text/html, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"hexin-v": v_code,
"Host": "data.10jqka.com.cn",
"Pragma": "no-cache",
"Referer": "http://data.10jqka.com.cn/funds/hyzjl/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
}
r = requests.get(url.format(page), headers=headers)
temp_df = pd.read_html(r.text)[0]
big_df = pd.concat([big_df, temp_df], ignore_index=True)
del big_df["序号"]
big_df.reset_index(inplace=True)
big_df["index"] = range(1, len(big_df) + 1)
if symbol == "即时":
big_df.columns = [
"序号",
"行业",
"行业指数",
"行业-涨跌幅",
"流入资金",
"流出资金",
"净额",
"公司家数",
"领涨股",
"领涨股-涨跌幅",
"当前价",
]
big_df["行业-涨跌幅"] = big_df["行业-涨跌幅"].str.strip("%")
big_df["领涨股-涨跌幅"] = big_df["领涨股-涨跌幅"].str.strip("%")
big_df["行业-涨跌幅"] = pd.to_numeric(big_df["行业-涨跌幅"], errors="coerce")
big_df["领涨股-涨跌幅"] = pd.to_numeric(big_df["领涨股-涨跌幅"], errors="coerce")
else:
big_df.columns = [
"序号",
"行业",
"公司家数",
"行业指数",
"阶段涨跌幅",
"流入资金",
"流出资金",
"净额",
]
return big_df
def stock_zygc_ym(symbol):
"""
益盟-F10-主营构成
http://f10.emoney.cn/f10/zbyz/1000001
"""
url = f"http://f10.emoney.cn/f10/zygc/{symbol}"
r = requests.get(url)
soup = BeautifulSoup(r.text, "lxml")
year_list = [
item.text.strip()
for item in soup.find(attrs={"class": "swlab_t"}).find_all("li")
]
big_df = pd.DataFrame()
for i, item in enumerate(year_list, 2):
temp_df = pd.read_html(r.text, header=0)[i]
temp_df.columns = [
"分类方向",
"分类",
"营业收入",
"营业收入-同比增长",
"营业收入-占主营收入比",
"营业成本",
"营业成本-同比增长",
"营业成本-占主营成本比",
"毛利率",
"毛利率-同比增长",
]
temp_df["报告期"] = item
big_df = pd.concat([big_df, temp_df], ignore_index=True)
big_df = big_df[
[
"报告期",
"分类方向",
"分类",
"营业收入",
"营业收入-同比增长",
"营业收入-占主营收入比",
"营业成本",
"营业成本-同比增长",
"营业成本-占主营成本比",
"毛利率",
"毛利率-同比增长",
]
]
return big_df