-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_crawler.py
executable file
·86 lines (66 loc) · 2.5 KB
/
basic_crawler.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
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
import tushare as ts
from pymongo import UpdateOne
from database import DB_CONN
from stock_util import get_trading_dates
"""
从tushare获取股票基础数据,保存到本地的MongoDB数据库中
"""
def crawl_basic(begin_date=None, end_date=None):
"""
抓取指定时间范围内的股票基础信息
:param begin_date: 开始日期
:param end_date: 结束日期
"""
if begin_date is None:
begin_date = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
if end_date is None:
end_date = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
all_dates = get_trading_dates(begin_date, end_date)
for date in all_dates:
try:
crawl_basic_at_date(date)
except:
print('抓取股票基本信息时出错,日期:%s' % date, flush=True)
def crawl_basic_at_date(date):
"""
从Tushare抓取指定日期的股票基本信息
:param date: 日期
"""
# 默认推送上一个交易日的数据
df_basics = ts.get_stock_basics(date)
# 如果当日没有基础信息,在不做操作
if df_basics is None:
return
update_requests = []
codes = set(df_basics.index)
for code in codes:
doc = dict(df_basics.loc[code])
try:
# 将20180101转换为2018-01-01的形式
time_to_market = datetime \
.strptime(str(doc['timeToMarket']), '%Y%m%d') \
.strftime('%Y-%m-%d')
totals = float(doc['totals'])
outstanding = float(doc['outstanding'])
doc.update({
'code': code,
'date': date,
'timeToMarket': time_to_market,
'outstanding': outstanding,
'totals': totals
})
update_requests.append(
UpdateOne(
{'code': code, 'date': date},
{'$set': doc}, upsert=True))
except:
print('发生异常,股票代码:%s,日期:%s' % (code, date), flush=True)
print(doc, flush=True)
if len(update_requests) > 0:
update_result = DB_CONN['basic'].bulk_write(update_requests, ordered=False)
print('抓取股票基本信息,日期:%s, 插入:%4d条,更新:%4d条' %
(date, update_result.upserted_count, update_result.modified_count), flush=True)
if __name__ == '__main__':
crawl_basic('2017-01-01', '2017-12-31')