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') today_opening_price = float(i.find_all('td')[1].text.replace(' ', '').strip()) today_highest_price = float(i.find_all('td')[2].text.replace(' ', '').strip()) today_lowest_price = float(i.find_all('td')[3].text.replace(' ', '').strip()) today_closing_price = float(i.find_all('td')[4].text.replace(' ', '').strip()) increase_rate = i.find_all('td')[5].text.replace(' ', '').strip() + '%' increase_amount = float(i.find_all('td')[6].text.replace(' ', '').strip()) turnover_rate = i.find_all('td')[7].text.replace(' ', '').strip() + '%' 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) sdt.save()
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(' ', '').strip()) today_highest_price = float(i.find_all('td')[2].text.replace(' ', '').strip()) today_lowest_price = float(i.find_all('td')[3].text.replace(' ', '').strip()) today_closing_price = float(i.find_all('td')[4].text.replace(' ', '').strip()) increase_rate = i.find_all('td')[5].text.replace(' ', '').strip() + '%' increase_amount = float(i.find_all('td')[6].text.replace(' ', '').strip()) turnover_rate = i.find_all('td')[7].text.replace(' ', '').strip() + '%' total_stock = int(i.find_all('td')[10].text.replace(' ', '').replace(',', '').strip()) circulation_stock = int(i.find_all('td')[12].text.replace(' ', '').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()
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(" ", "").strip()) today_highest_price = float(i.find_all("td")[2].text.replace(" ", "").strip()) today_lowest_price = float(i.find_all("td")[3].text.replace(" ", "").strip()) today_closing_price = float(i.find_all("td")[4].text.replace(" ", "").strip()) increase_rate = i.find_all("td")[5].text.replace(" ", "").strip() + "%" increase_amount = float(i.find_all("td")[6].text.replace(" ", "").strip()) turnover_rate = i.find_all("td")[7].text.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, ) sdt.save()
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.strip(), '%Y-%m-%d') try: today_opening_price = float( i.find_all('td')[1].text.replace(' ', '').strip()) today_highest_price = float( i.find_all('td')[2].text.replace(' ', '').strip()) today_lowest_price = float( i.find_all('td')[3].text.replace(' ', '').strip()) today_closing_price = float( i.find_all('td')[4].text.replace(' ', '').strip()) increase_rate = i.find_all('td')[5].text.replace( ' ', '').strip() + '%' increase_amount = float( i.find_all('td')[6].text.replace(' ', '').strip()) turnover_rate = i.find_all('td')[7].text.replace( ' ', '').strip() + '%' total_stock = int( i.find_all('td')[10].text.replace(' ', '').replace(',', '').strip()) circulation_stock = int( i.find_all('td')[12].text.replace(' ', '').replace(',', '').strip()) turnover_amount_msg = i.find_all('td')[9].text.replace( ' ', '').strip() turnover_amount = 0 if u'万' in turnover_amount_msg: turnover_amount = int( float(turnover_amount_msg.replace(u'万', ''))) elif u'亿' in turnover_amount_msg: turnover_amount = int( float(turnover_amount_msg.replace(u'亿', '')) * 10000) if turnover_amount_msg.isdigit() and int( turnover_amount_msg) == 0: return except Exception as 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 check_exists(stock_number, date): sdt = SDT.objects(Q(stock_number=stock_number) & Q(date=date)).next() if not sdt.total_stock or not sdt.circulation_stock or not sdt.turnover_amount: sdt.total_stock = total_stock sdt.circulation_stock = circulation_stock sdt.turnover_amount = turnover_amount sdt.save() else: # 添加股本相关数据 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, turnover_amount=turnover_amount) sdt.save()