def get_market(): #util.clean_market(0) url = 'http://pregao-online.bmfbovespa.com.br/Cotacoes.aspx' soup = BeautifulSoup(urlfetch.fetch(url, deadline=50).content, 'lxml') rate = util.get_exchange() #dt = get_datetime() dt = datetime.datetime.now(tz.tzstr('EBST3EBDT')) market = Market(ref=0, date=dt.date(), time=dt.time(), exchange_rate=rate) market.put() table = soup('table', attrs={'id': 'ctl00_DefaultContent_GrdCarteiraIndice'})[0] for tr in table('tr')[1:]: tds = tr('td') code = str(tds[0].string) name = util.clean_string(tds[1].string) value = util.get_float(tds[2].string) diff = util.get_float(tds[3].text.strip()) stock = Stock(name=util.get_or_create_name(0, code, name), value=value, diff=diff, market=market.key()) stock.put()
def get_market(): #util.clean_market(1) #dt = get_datetime() dt = datetime.datetime.now(tz.tzstr('EST5EDT')) market = Market(ref=1, date=dt.date(), time=dt.time()) market.put() url = 'http://www.barchart.com/stocks/nasdaq100.php?view=main' soup = BeautifulSoup(urlfetch.fetch(url, deadline=30).content, 'lxml') table = soup('table', attrs={'id': 'dt1'})[0] for tr in table('tr')[1:]: tds = tr('td') code = str(tds[0].text) name = str(tds[1].text) value = float(tds[2].text.replace(',', '')) text_replace = tds[4].text.replace(',', '').replace('%', '') diff = float(text_replace) if text_replace != 'unch' else 0.0 stock = Stock(name=util.get_or_create_name(1, code, name), value=value, diff=diff, market=market.key()) stock.put()