def collect_his_trading(stock_number, stock_name):
    if stock_number.startswith('6'):
        req_url = history_trading.format(stock_number+'01')
    else:
        req_url = history_trading.format(stock_number+'02')
    his_html = send_request(req_url)

    his_soup = BeautifulSoup(his_html, 'lxml')
    his_table = his_soup.find('table', id='tablefont')

    if his_table:
        his_data = his_table.find_all('tr')[1:]
        for i in his_data:
            date = datetime.datetime.strptime(i.find('p', class_='date').text, '%Y-%m-%d')
            try:
                today_opening_price = float(i.find_all('td')[1].text.replace('&nbsp', '').strip())
                today_highest_price = float(i.find_all('td')[2].text.replace('&nbsp', '').strip())
                today_lowest_price = float(i.find_all('td')[3].text.replace('&nbsp', '').strip())
                today_closing_price = float(i.find_all('td')[4].text.replace('&nbsp', '').strip())
                increase_rate = i.find_all('td')[5].text.replace('&nbsp', '').strip() + '%'
                increase_amount = float(i.find_all('td')[6].text.replace('&nbsp', '').strip())
                turnover_rate = i.find_all('td')[7].text.replace('&nbsp', '').strip() + '%'
                total_stock = int(i.find_all('td')[10].text.replace('&nbsp', '').replace(',', '').strip())
                circulation_stock = int(i.find_all('td')[12].text.replace('&nbsp', '').replace(',', '').strip())
            except Exception, e:
                if '--' not in str(e):
                    logging.error('Collect %s %s trading data failed:%s' % (stock_number, str(date), e))
                continue

            if float(increase_rate.replace('%', '')) == 0.0 and float(turnover_rate.replace('%', '')) == 0.0:
                # 去掉停牌期间的行情数据
                continue

            if not check_exists(stock_number, date):
                sdt = SDT(stock_number=stock_number, stock_name=stock_name, date=date,
                          today_opening_price=today_opening_price, today_highest_price=today_highest_price,
                          today_lowest_price=today_lowest_price, today_closing_price=today_closing_price,
                          increase_rate=increase_rate, increase_amount=increase_amount, turnover_rate=turnover_rate,
                          total_stock=total_stock, circulation_stock=circulation_stock)
                sdt.save()
            else:  # 添加股本相关数据
                sdt = SDT.objects(Q(stock_number=stock_number) & Q(date=date)).next()
                if not sdt.total_stock:
                    sdt.total_stock = total_stock
                if not sdt.circulation_stock:
                    sdt.circulation_stock = circulation_stock

                sdt.save()
Пример #2
0
def collect_his_trading(stock_number, stock_name):
    if stock_number.startswith('6'):
        req_url = history_trading.format(stock_number + '01')
    else:
        req_url = history_trading.format(stock_number + '02')
    his_html = send_request(req_url)

    his_soup = BeautifulSoup(his_html, 'lxml')
    his_table = his_soup.find('table', id='tablefont')

    if his_table:
        his_data = his_table.find_all('tr')[1:]
        for i in his_data:
            date = datetime.datetime.strptime(
                i.find('p', class_='date').text, '%Y-%m-%d')
            try:
                today_opening_price = float(
                    i.find_all('td')[1].text.replace('&nbsp', '').strip())
                today_highest_price = float(
                    i.find_all('td')[2].text.replace('&nbsp', '').strip())
                today_lowest_price = float(
                    i.find_all('td')[3].text.replace('&nbsp', '').strip())
                today_closing_price = float(
                    i.find_all('td')[4].text.replace('&nbsp', '').strip())
                increase_rate = i.find_all('td')[5].text.replace(
                    '&nbsp', '').strip() + '%'
                increase_amount = float(
                    i.find_all('td')[6].text.replace('&nbsp', '').strip())
                turnover_rate = i.find_all('td')[7].text.replace(
                    '&nbsp', '').strip() + '%'
                total_stock = int(
                    i.find_all('td')[10].text.replace('&nbsp',
                                                      '').replace(',',
                                                                  '').strip())
                circulation_stock = int(
                    i.find_all('td')[12].text.replace('&nbsp',
                                                      '').replace(',',
                                                                  '').strip())
            except Exception, e:
                if '--' not in str(e):
                    logging.error('Collect %s %s trading data failed:%s' %
                                  (stock_number, str(date), e))
                continue

            if float(increase_rate.replace('%', '')) == 0.0 and float(
                    turnover_rate.replace('%', '')) == 0.0:
                # 去掉停牌期间的行情数据
                continue

            if not check_exists(stock_number, date):
                sdt = SDT(stock_number=stock_number,
                          stock_name=stock_name,
                          date=date,
                          today_opening_price=today_opening_price,
                          today_highest_price=today_highest_price,
                          today_lowest_price=today_lowest_price,
                          today_closing_price=today_closing_price,
                          increase_rate=increase_rate,
                          increase_amount=increase_amount,
                          turnover_rate=turnover_rate,
                          total_stock=total_stock,
                          circulation_stock=circulation_stock)
                sdt.save()
            else:  # 添加股本相关数据
                sdt = SDT.objects(Q(stock_number=stock_number)
                                  & Q(date=date)).next()
                if not sdt.total_stock:
                    sdt.total_stock = total_stock
                if not sdt.circulation_stock:
                    sdt.circulation_stock = circulation_stock

                sdt.save()