def get_predict_share_Sina(code, mtype): """ mtype: eps--每股收益, sales--营业收入, np--净利润, roe--净资产收益率 """ pn = 1 DF = pd.DataFrame() ws._write_head() while True: try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vPerformancePrediction/kind/{0}/index.phtml?symbol={1}&p={2}'.format( mtype, code, pn) r = requests.get(url) r = r.content.decode('gbk') html = BeautifulSoup(r, 'lxml') text = html.find(id='dataTable') df = pd.read_html(str(text), header=0)[0] if df.empty is True: break else: pn = pn + 1 DF = DF.append(df) except: break DF = DF.applymap(lambda x: np.where(x == '--', np.nan, x)) DF = DF.set_index('股票代码') DF.index = DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) return DF
def margin_share(code, begin, end): """ datetime:日期 如2017-06-16 ----------------- Return: DataFrame """ DF = pd.DataFrame() ws._write_head() try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vInvestConsult/kind/rzrq/index.phtml?symbol={0}&bdate={1}&edate={2}'.format( code, begin, end) #print(url) r = requests.get(url) r = r.content.decode('gbk') html = BeautifulSoup(r, 'lxml') sarr = html.find(id='dataTable') df = pd.read_html(str(sarr), header=None, skiprows=3)[0] df = df.applymap(lambda x: np.where(x == '--', np.nan, x)) df = df.drop(0, axis=1) #print(df.head()) df.columns = name df = df.set_index('date') return df except Exception as e: print(e) pass
def dzjy(): """ 查询大宗交易的数据,主要是当前数据的前20页的数据 ----------------- Return: DataFrame """ pn = 1 DF = pd.DataFrame() ws._write_head() while True: try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vInvestConsult/kind/dzjy/index.phtml?num=60&p={0}'.format( pn) r = requests.get(url) #print(url) r = r.content.decode('gbk') html = BeautifulSoup(r, 'lxml') sarr = html.find(id='dataTable') df = pd.read_html(str(sarr), header=0)[0] DF = DF.append(df) pn = pn + 1 if pn > 20: break if df.empty is True: break except: break DF = DF.applymap(lambda x: np.where(x == '--', np.nan, x)) DF = DF.drop_duplicates() return DF
def grow(year,quarter): pn=1 DF=pd.DataFrame() ws._write_head() while True: try: ws._write_console() url='http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/grow/index.phtml?s_i=&s_a=&s_c=&reportdate={0}&quarter={1}&p={2}'.format(year,quarter,pn) r=requests.get(url,headers=hds()) r=r.content.decode('gbk') html=BeautifulSoup(r,'lxml') text=html.find(id='dataTable') df=pd.read_html(str(text),header=0)[0] if df.empty is True: break else: pn = pn + 1 DF =DF.append(df) except: break DF=DF.applymap(lambda x:np.where(x=='--',np.nan,x)) DF=DF.set_index('股票代码') DF.index=DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) DF['date']=str(year)+'_'+str(quarter).zfill(2) name=list(DF.columns) name.remove('股票名称') name.remove('date') for label in name: DF[label]=DF[label].astype(float) return DF
def xsjj(begin,end): """ 查询限售解禁的数据,主要是当前数据的前20页的数据 ----------------- Return: DataFrame """ pn=1 DF=pd.DataFrame() ws._write_head() while True: try: ws._write_console() url='http://vip.stock.finance.sina.com.cn/q/go.php/vInvestConsult/kind/xsjj/index.phtml?bdate={0}&edate={1}&showall=%CF%D4%CA%BE%C8%AB%B2%BF&p={2}'.format(begin,end,pn) r=requests.get(url) #print(url) r=r.content.decode('gbk') html=BeautifulSoup(r,'lxml') sarr=html.find(id='dataTable') df=pd.read_html(str(sarr),header=0)[0] DF =DF.append(df) pn=pn+1 if pn > 50: break if df.empty is True: break except: break DF=DF.applymap(lambda x:np.where(x=='--',np.nan,x)) DF=DF.drop_duplicates() DF['代码']=DF['代码'].map(lambda x:str(x).split('.')[0].zfill(6)) return DF
def dzjy_share(code, begin=None, end=None): """ 查询某一区间的股票的大宗交易情况和相关数据. datetime:日期 如2017-06-16 ----------------- Return: DataFrame """ if code[0] in ['0', '2', '3']: code = 'sz' + code if code[0] in ['6', '9']: code = 'sh' + code if end == None or (dt.datetime.strptime(end, '%Y-%m-%d') < dt.datetime.strptime(begin, '%Y-%m-%d')): end = dt.datetime.strftime(dt.datetime.today(), '%Y-%m-%d') if begin == None: begin = dt.datetime.strftime( dt.datetime.today() - dt.timedelta(days=360), '%Y-%m-%d') pn = 1 DF = pd.DataFrame() ws._write_head() while True: try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vInvestConsult/kind/dzjy/index.phtml?symbol={0}&bdate={1}&edate={2}&p={3}'.format( code, begin, end, pn) #print(url) r = requests.get(url) r = r.content.decode('gbk') html = BeautifulSoup(r, 'lxml') sarr = html.find(id='dataTable') df = pd.read_html(str(sarr), header=0)[0] DF = DF.append(df) pn = pn + 1 if pn > 20: break if df.empty is True: break except: break DF = DF.applymap(lambda x: np.where(x == '--', np.nan, x)) DF = DF.drop_duplicates() return DF
def holders(year, quarter, mytype): """ mytype:主要有四种类型,jgcg,jjzc,sbzc,qfii year: 年份 quarter:季度,分为1,2,3,4 ----------------- Return: DataFrame """ pn = 1 DF = pd.DataFrame() ws._write_head() while True: try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vComStockHold/kind/{3}/index.phtml?symbol=%D6%A4%C8%AF%BC%F2%B3%C6%BB%F2%B4%FA%C2%EB&reportdate={0}&quarter={1}&p={2}'.format( year, quarter, pn, mytype) r = requests.get(url) r = r.content.decode('gbk') html = BeautifulSoup(r, 'lxml') text = html.find(id='dataTable') df = pd.read_html(str(text), header=0)[0] #print(df) if df.empty is True: break else: pn = pn + 1 DF = DF.append(df) except: break DF = DF.applymap(lambda x: np.where(x == '--', np.nan, x)) DF = DF.drop_duplicates() print(DF) if mytype in ['jjzc', 'sbzc', 'qfii']: DF = DF.drop([1, 2], axis=0) try: DF = DF.set_index('证券代码') DF.index = DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) except: DF = DF.set_index('代码') DF.index = DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) finally: pass return DF
def margindetail(begin, end): """ datetime:日期 如2017-06-16 ----------------- Return: DataFrame """ days = pd.date_range(begin, end, freq='B') DF = pd.DataFrame() ws._write_head() for day in days: try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vInvestConsult/kind/rzrq/index.phtml?tradedate={0}'.format( day.strftime('%Y-%m-%d')) r = requests.get(url) r = r.content.decode('gbk') html = lxml.html.parse(StringIO(r)) res = html.xpath("//div[@class='list']/table[2]//tr[position()>3]") sarr = [etree.tostring(node) for node in res] #sarr = ''.join(sarr) #print(sarr) sarr = ''.join(str(sarr)) sarr = '<table>%s</table>' % sarr df = pd.read_html(sarr, header=None)[0] df['day'] = day #print(df) if df.empty is False: DF = DF.append(df) except: pass DF = DF.applymap(lambda x: np.where(x == '--', np.nan, x)) DF = DF.drop_duplicates() name.append('date') name.insert(0, 'code') DF.columns = name DF['code'] = DF['code'].map(lambda x: str(x).split('.')[0].zfill(6)) DF = DF.set_index(['code', 'date']) #print(DF) return DF
def forcast_share(code): DF=pd.DataFrame() ws._write_head() try: ws._write_console() url='http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/performance/index.phtml?symbol={0}'.format(code) r=requests.get(url,headers=hds()) r=r.content.decode('gbk') html=BeautifulSoup(r,'lxml') text=html.find(id='dataTable') df=pd.read_html(str(text))[0] if df.empty is False: DF =DF.append(df) DF=DF.applymap(lambda x:np.where(x=='--',np.nan,x)) DF=DF.drop(8,axis=1) DF.columns=['code','name','type','pdate','rpdate','summary','eps_l','Up_Down'] DF=DF.set_index('code') DF.index=DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) return DF except Exception as e: print(e)
def holders_share(code, year, quarter, mytype): """ mytype:主要有四种类型,jgcg,jjzc,sbzc,qfii year: 年份 quarter:季度,分为1,2,3,4 ----------------- Return: DataFrame """ ws._write_head() try: ws._write_console() url = 'http://vip.stock.finance.sina.com.cn/q/go.php/vComStockHold/kind/{3}/index.phtml?symbol={0}&reportdate={1}&quarter={2}'.format( code, year, quarter, mytype) r = requests.get(url) r = r.content.decode('gbk') html = BeautifulSoup(r, 'lxml') text = html.find(id='dataTable') DF = pd.read_html(str(text), header=0)[0] except: pass DF = DF.applymap(lambda x: np.where(x == '--', np.nan, x)) DF = DF.drop_duplicates() #print(DF) if mytype in ['jjzc', 'sbzc', 'qfii']: DF = DF.drop([1, 2], axis=0) try: DF = DF.set_index('证券代码') DF.index = DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) except: DF = DF.set_index('代码') DF.index = DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) finally: pass return DF
def get_researchRate_Sina(mtype, period=10, rate=None, srate=None, zf=None): """ 投资评级选股 ------------------------------ mtype: hy,dy,gn preiod: -1,10,30,60 rate:5,4,3,2,1 srate:5,4,3,2,1 zf:1,2,3,4,5 ----------------------------- Return: DataFrame code: 股票代码 name :股票名称 rate :最新评级 target: 目标价 date :评级时间 srate :综合评价 chage :平均涨幅 industry:行业 """ burl = 'http://vip.stock.finance.sina.com.cn/q/go.php/vIR_CustomSearch/index.phtml?sr_p=%s' % period if mtype == 'hy': name = input("Enter name in %s" % mtype) burl = burl + '&industry=' + hy[name] elif mtype == 'dy': name = input("Enter name in %s" % mtype) burl = burl + '&zone=' + dy[name] elif mtype == 'gn': name = input("Enter name in %s" % mtype) burl = burl + '&concept=' + gn[name] else: pass if rate is not None: burl = burl + '&rating=%s' % rate if srate is not None: burl = burl + '&srating=%s' % srate if zf is not None: burl = burl + '&sprice=%s' % zf #burl=burl+'&p={0}' #print(burl) DF = pd.DataFrame() ws._write_head() pageno = 1 while True: #print(pageno) url = burl + '&p={0}'.format(pageno) try: ws._write_console() #print(url) r = requests.get(url) text = r.content.decode('gbk') items = lxml.html.parse(StringIO(text)) tb = items.xpath('//table[@class="list_table"]//tr[position()>1]') sarr = [etree.tostring(node) for node in tb] sarr = '<table>%s</table>' % sarr df = pd.read_html(str(text), header=None)[0] df = df.drop(0, axis=0) if df.empty: #print("No data") break else: DF = DF.append(df) pageno = pageno + 1 except: #print(url) break try: DF = DF.drop([7, 9, 10, 11, 12, 13], axis=1) DF.columns = [ 'code', 'name', 'rate', 'target', 'srate', 'chage', 'industry' ] except: DF.columns = [ 'code', 'name', 'rate', 'target', 'date', 'srate', 'chage', 'industry' ] pass DF = DF.reset_index(drop=True) #DF=DF.applymap(lambda x:np.where(x=='--',np.nan,x)) #DF=DF.set_index('股票代码') #DF.index=DF.index.map(lambda x: str(x).split('.')[0].zfill(6)) DF = DF.replace('--', np.nan) DF['target'] = DF['target'].map(lambda x: float(x)) return DF