def main(self): if not self._validity: print "SZX this is Saturday or Monday!" return 0 coll_in = Mongodb("192.168.251.95", 27017, "ada", "base_margin_trading") coll_stock = Mongodb("192.168.251.95", 27017, "ada", "base_stock") coll_fund = Mongodb("192.168.251.95", 27017, "fund", "base_fund") url = "http://www.szse.cn/szseWeb/FrontController.szse?randnum=&" t = lambda v: "%.4f" % float(v) for page in range(1, 30): break_point = False html = self.get_html(url + self._query_string.format(self._query_date, page), encoding=True) for it in self.extract(html): # print it[0], it[1], it[2], it[3], it[4], it[5], it[6] break_point = True secu_cd = secu_code(it[0], coll_stock, coll_fund) fiba_bre = szx_fiba_bre(secu_cd, coll_in, self._query_date) sema_bre = szx_sema_bre(secu_cd, coll_in, self._query_date) # 本日融资偿还额 = 前日融资余额 + 本日融资买入- 本日融资余额(元) (fi.re = fi.ba(上期) + fi.bu - fi.ba) # 融券偿还量 = 融券卖出量 + 融券余量(上期) - 融券余量 (se.re = se.so + se.ma(上期) - se.ma) szx_fs_data = { "secu": secu_cd or it[0], "date": self._query_date, "total": t(it[6]), "stat": 2, "typ": "szx", "crt": datetime.now(), "fi": {"ba": t(it[2]), "bu": t(it[1]), "re": t(float(it[1]) + fiba_bre - float(it[2]))}, "se": { "ba": t(it[5]), "ma": t(it[4]), "so": t(it[3]), "re": t(float(it[3]) + sema_bre - float(it[4])), }, "upt": datetime.now(), } print szx_fs_data if not coll_in.get({"secu": secu_cd or it[0], "date": self._query_date, "typ": "szx"}): print coll_in.insert(szx_fs_data) if not break_point: break print u"szx [%s] 融资融券交易明细 day update: %d page done!" % (self._query_date, page) # break coll_in.disconnect() coll_stock.disconnect() coll_fund.disconnect()
def insert_db(self, total_data): coll_in = Mongodb('192.168.251.95', 27017, 'ada', 'base_margin_trading') coll_stock = Mongodb('192.168.251.95', 27017, 'ada', 'base_stock') coll_fund = Mongodb('192.168.251.95', 27017, 'fund', 'base_fund') sql_db = MySQLClient("192.168.251.95", "python_team", "python_team", "ada-fd") print '\tnow start to insert mongodb, waiting......' d = (lambda v: '%.4f' % float(v)) for pdt in total_data: # 信用交易日期 标的证券代码 标的证券简称 本日融资余额(元) 本日融资买入额(元) # 本日融资偿还额(元) 本日融券余量 本日融券卖出量 本日融券偿还量 secu_cd = secu_code(pdt[1], coll_stock, coll_fund) trade_date = '-'.join([pdt[0][:4], pdt[0][4:6], pdt[0][6:]]) uid = str(uuid.uuid5(uuid.NAMESPACE_DNS, ''.join(self._valid(pdt)).encode('u8'))) data = { 'secu': secu_cd or pdt[1], 'date': trade_date, 'total': d(int(pdt[3])), 'stat': 2, 'typ': 'sha', 'crt': datetime.now(), 'uuid': uid, 'fi': { 'ba': d(pdt[3]), 'bu': d(pdt[4]), 're': d(pdt[5]) }, 'se': { 'ba': '0.0000', 'ma': d(pdt[6]), 'so': d(pdt[7]), 're': d(pdt[8]) }, 'upt': datetime.now() } if coll_in.get({'uuid': uid, 'typ': 'sha'}, {'secu': 1}): continue elif secu_cd is None: coll_in.insert(data) else: seba = sha_seba(secu_cd, pdt[6], trade_date, sql_db) if seba is not None: data['total'] = d(int(pdt[3]) + seba) data['se']['ba'] = d(seba) coll_in.insert(data) coll_in.disconnect() coll_stock.disconnect() sql_db.disconnect() print '\tinsert all done!'
def main(self): if not self._validity: print 'SZX this is Saturday or Monday!' return 0 coll_in = Mongodb('192.168.251.95', 27017, 'ada', 'base_margin_trading') coll_stock = Mongodb('192.168.251.95', 27017, 'ada', 'base_stock') coll_fund = Mongodb('192.168.251.95', 27017, 'fund', 'base_fund') url = 'http://www.szse.cn/szseWeb/FrontController.szse?randnum=&' t = lambda v: '%.4f' % float(v) for page in range(1, 30): break_point = False html = self.get_html( url + self._query_string.format(self._query_date, page), encoding=True) for it in self.extract(html): # print it[0], it[1], it[2], it[3], it[4], it[5], it[6] break_point = True secu_cd = secu_code(it[0], coll_stock, coll_fund) fiba_bre = szx_fiba_bre(secu_cd, coll_in, self._query_date) sema_bre = szx_sema_bre(secu_cd, coll_in, self._query_date) # 本日融资偿还额 = 前日融资余额 + 本日融资买入- 本日融资余额(元) (fi.re = fi.ba(上期) + fi.bu - fi.ba) # 融券偿还量 = 融券卖出量 + 融券余量(上期) - 融券余量 (se.re = se.so + se.ma(上期) - se.ma) szx_fs_data = { 'secu': secu_cd or it[0], 'date': self._query_date, 'total': t(it[6]), 'stat': 2, 'typ': 'szx', 'crt': datetime.now(), 'fi': { 'ba': t(it[2]), 'bu': t(it[1]), 're': t(float(it[1]) + fiba_bre - float(it[2])) }, 'se': { 'ba': t(it[5]), 'ma': t(it[4]), 'so': t(it[3]), 're': t(float(it[3]) + sema_bre - float(it[4])) }, 'upt': datetime.now() } print szx_fs_data if not coll_in.get({ 'secu': secu_cd or it[0], 'date': self._query_date, 'typ': 'szx' }): print coll_in.insert(szx_fs_data) if not break_point: break print u'szx [%s] 融资融券交易明细 day update: %d page done!' % ( self._query_date, page) # break coll_in.disconnect() coll_stock.disconnect() coll_fund.disconnect()