def getMain(): ctime = int(time.time()) date = datetime.datetime.today() date = str(local2utc(date,-21)).split(' ')[0] + ' 00:00:00' typenames = ['nasdaq','newyork'] for typename in typenames: url = 'http://quotes.money.163.com/us/service/usrank.php?host=/us/service/usrank.php&page=0&query=UPDATE:_exists_true;PRICE:_exists_true;typename:' + typename + '&fields=no,SYMBOL,NAME,PRICE,UPDOWN,PERCENT,WEEK52_HIGH,WEEK52_LOW,TCAP,PE,VOLUME&sort=PERCENT&order=desc&count=5000&type=query&callback=callback_1074842233&req=02057' response = requests.get(url) try: dicR = json.loads(response.text[20:-1].replace('\\','').replace('\r','').replace('\n','')) except Exception,e: print e print response.text.split("(")[1].split(")")[0].replace('\\','').replace('\r','').replace('\n','')[99600:1002000] for stockDetail in dicR['list']: sqlSelect = 'select price from stocks where date=\'' + date +'\' and stockName=\'' + stockDetail['SYMBOL'] +'\';' isIn = cur.selectData(sqlSelect) if len(isIn)==0: sql = 'insert into stocks (stockName,price,vol,date,ctime) values (\'' + stockDetail['SYMBOL'] + '\', ' + str(stockDetail['PRICE']) + ',' + str(stockDetail['VOLUME']) + ', \'' + date + '\',' + str(ctime) + ');' elif float('%0.2f'%isIn[0][0])==float('%0.2f'%stockDetail['PRICE']): continue else: print float('%0.2f'%isIn[0][0]),float('%0.2f'%stockDetail['PRICE']) sql = 'UPDATE stocks set price=' + str(stockDetail['PRICE']) + ', volume=' + str(stockDetail['VOLUME']) + ', ctime='+ str(ctime) +' WHERE date=\'' + date +'\' and stockName=\'' + stockDetail['SYMBOL'] +'\';' try : cur.executeDB(sql) except Exception,e: print e
def getMain(): ctime = int(time.time()) date = datetime.datetime.today() date = str(local2utc(date, -21)).split(' ')[0] + ' 00:00:00' typenames = ['nasdaq', 'newyork'] for typename in typenames: url = 'http://quotes.money.163.com/us/service/usrank.php?host=/us/service/usrank.php&page=0&query=UPDATE:_exists_true;PRICE:_exists_true;typename:' + typename + '&fields=no,SYMBOL,NAME,PRICE,UPDOWN,PERCENT,WEEK52_HIGH,WEEK52_LOW,TCAP,PE,VOLUME&sort=PERCENT&order=desc&count=5000&type=query&callback=callback_1074842233&req=02057' response = requests.get(url) try: dicR = json.loads(response.text[20:-1].replace('\\', '').replace( '\r', '').replace('\n', '')) except Exception, e: print e print response.text.split("(")[1].split(")")[0].replace( '\\', '').replace('\r', '').replace('\n', '')[99600:1002000] for stockDetail in dicR['list']: sqlSelect = 'select price from stocks where date=\'' + date + '\' and stockName=\'' + stockDetail[ 'SYMBOL'] + '\';' isIn = cur.selectData(sqlSelect) if len(isIn) == 0: sql = 'insert into stocks (stockName,price,vol,date,ctime) values (\'' + stockDetail[ 'SYMBOL'] + '\', ' + str(stockDetail['PRICE']) + ',' + str( stockDetail['VOLUME']) + ', \'' + date + '\',' + str( ctime) + ');' elif float('%0.2f' % isIn[0][0]) == float('%0.2f' % stockDetail['PRICE']): continue else: print float('%0.2f' % isIn[0][0]), float('%0.2f' % stockDetail['PRICE']) sql = 'UPDATE stocks set price=' + str( stockDetail['PRICE'] ) + ', volume=' + str(stockDetail['VOLUME']) + ', ctime=' + str( ctime ) + ' WHERE date=\'' + date + '\' and stockName=\'' + stockDetail[ 'SYMBOL'] + '\';' try: cur.executeDB(sql) except Exception, e: print e
kw = kw[:-1] if kw == '': break try: url = 'http://www.nasdaq.com/symbol/' + kw + '/option-chain/most-active' print url content = requests.get(url) content = BeautifulSoup.BeautifulSoup(content.text) datas = content.findAll( 'div', {'class': 'OptionsChain-chart borderAll thin'}) tdDatas = datas[0].findAll('td', {'class': 'most-active-col1'}) for tdElm in tdDatas[1:]: ctime = int(time.time()) date = datetime.datetime.today() date = str(local2utc(date, -9.5)).split(' ')[0] + ' 00:00:00' try: tds = tdElm.parent.findAll('td') name = tds[0].a['href'].split('option-chain/')[1] dic[name] = {} dic[name]['optionType'] = tds[1].contents[0] dic[name]['stockName'] = tds[2].contents[0] dic[name]['strike'] = tds[3].contents[0] dic[name]['vol'] = tds[7].contents[0] dic[name]['openInt'] = tds[8].contents[0] dic[name]['bid'] = tds[9].contents[0] dic[name]['ask'] = tds[10].contents[0] expiredAt = name.split('C')[0].split('P')[0] dic[name][ 'expired'] = '20' + expiredAt[:2] + '-' + expiredAt[
#coding:utf-8 import requests,time import BeautifulSoup import mySQLClass,sys import datetime from utc2local import local2utc dic = {} cur = mySQLClass.MySQLClass('localhost',3306,'root','814155356Mysql') cur.connectDB() cur.selectDB('optionAna') date = datetime.datetime.today() date = str(local2utc(date,-9.5)).split(' ')[0] + ' 00:00:00' expired = str(datetime.datetime.strptime(date,'%Y-%m-%d %H:%M:%S') + datetime.timedelta(days=5)) sql = 'select stockName,sum(if(optionType="CALL",vol,0)),sum(if(optionType="PUT",vol,0)),sum(if(optionType="CALL",vol,0))/sum(if(optionType="PUT",vol,0))-1 from option_most_active where date>="' + date + '" and expired>="' + expired + '" group by stockName;' result = cur.selectData(sql) for line in result: stockName,callVol,putVol,div = line print stockName,callVol,putVol,div