def get_month_boxoffice(month=None): if month == None: result = ts.month_boxoffice().to_json() else: try: result = ts.month_boxoffice(month).to_json() except Exception as e: result = json.dumps({"error":True,"message":"can not get the data, format date as YYYY-M"}) return result
def job_9(): try: print("I'm working......电影票房") # 实时票房 realtime_boxoffice = ts.realtime_boxoffice() data = pd.DataFrame(realtime_boxoffice) data.to_sql('realtime_boxoffice',engine,index=True,if_exists='replace') print("实时票房......done") # 每日票房 day_boxoffice = ts.day_boxoffice() data = pd.DataFrame(day_boxoffice) data.to_sql('day_boxoffice',engine,index=True,if_exists='replace') print("每日票房......done") # 月度票房 month_boxoffice = ts.month_boxoffice() data = pd.DataFrame(month_boxoffice) data.to_sql('month_boxoffice',engine,index=True,if_exists='replace') print("月度票房......done") # 影院日度票房 day_cinema = ts.day_cinema() data = pd.DataFrame(day_cinema) data.to_sql('day_cinema',engine,index=True,if_exists='replace') print("影院日度票房......done") except Exception as e: print(e)
def get_movie(): movies = [] for m in range(1,11): df = ts.month_boxoffice('2018-'+str(m)) movies.append(df) res = pd.concat(movies) return res
def month_boxoffice(engine, ddate=None): tbl = "month_boxoffice" tsl.log(tbl + " start...") try: df = ts.month_boxoffice(ddate) df['date'] = ddate df.to_sql(tbl, engine, if_exists='append') tsl.log(tbl + " done") except BaseException, e: print e tsl.log(tbl + " error")
def month_box_office(): df = ts.month_boxoffice() f.write('\n### 月度票房\n\n') table_head = '|影片名|排名|上映日期|单月票房(万)|口碑指数|\n|-|-|-|-|-|\n' f.write(table_head) for i in range(len(df)): txt = '|' + df['MovieName'][i] + '|' + df['Irank'][i] + '|' + df['releaseTime'][i] + '|' + \ df['boxoffice'][i] + '|' + df['WomIndex'][i] + '|\n' f.write(txt) pass pass
def get_month_boxoffice(date=None): df = ts.month_boxoffice(date) if df is not None: if date is None: date = get_last_month() df.insert(0, 'date', date) res = df.to_sql(fun_month_box_office, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取单月份:{0} 电影票房数据: {1} '.format(date, msg) + '\n') else: print('获取单月份:{0} 电影票房数据: {1} '.format(date, 'None') + '\n')
def monthly_box_office(year): DTS=[] mbo_path='D:\\ts\\box_office\monthly_boxoffice_%s.csv'%year for month in range(12): preDTS=ts.month_boxoffice('%s-%s'%(year,month+1)) preDTS['month']=month+1 DTS.append(preDTS) DTS=pd.concat(DTS) if os.path.exists(mbo_path ): DTS.to_csv(mbo_path, encoding='gbk',mode='a',header=None) else: DTS.to_csv(mbo_path, encoding='gbk')
def updateMonthBoxoffice(con, year: int, month: int): import share.model.dao.boxoffice.MonthBoxoffice as Model dateString = "{year:04d}-{month:02d}".format(year=year,month=month) logging.debug("Updating month bboxoffice of {}".format(dateString)) df = ts.month_boxoffice(date=dateString,retry_count=16) res = [] for _, row in df.iterrows(): obj = Model.rowToORM(row, year=year, month=month) if obj is not None: res.append(obj) Base.metadata.create_all(con.engine) con.save_all(res) return
def month_boxoffice(month): """ 查询当月票房 @param month: 查询月份YYYY-M @return: 指定月份的票房信息 """ # 文件路径 file_path = './cache/ts/ts_month_boxoffice_%s.csv' % month # 如果存在数据文件,则直接读取 if os.path.exists(file_path): logcm.print_info('读取缓存数据...') # 读取文件 df_month = pd.read_csv(file_path) else: # 取得票房数据 df_month = ts.month_boxoffice(month) # 保存到文件 df_month.to_csv(file_path, index=False) return df_month
import tushare as ts import numpy as np import pandas as pd import matplotlib.pyplot as plt df = ts.month_boxoffice('2017-8') colors = ['red', 'yellow', 'blue'] print df['boxoffice'] pie = plt.pie(df['boxoffice'], labels=df['MovieName'], autopct='%1.1f%%', startangle=0, colors=colors) plt.axis('equal') plt.show() #data=pd.read_csv('1.txt',sep='\t') #print data
import tushare as ts # 实时票房 ts.realtime_boxoffice() # 每日票房 ts.day_boxoffice() #取上一日的数据 ts.day_boxoffice('2010-01-01') #取指定日期的数据 # 月度票房 ts.month_boxoffice() #取上一月票房数据 ts.month_boxoffice('2019-02') #此月数据 # 影院日度票房 ts.day_cinema() #取上一日全国影院票房排行数据 df = ts.day_cinema('2019-02-05') # 指定日期的数据 2019年春节 df.to_excel('movie.xlsx') # pip install openpyxl
import tushare as ts from sqlalchemy import create_engine token = ts.set_token( 'c723069dd4a25402d05ea6afad36da2937111adf012f8258abb5f7e05936e575') #engine = create_engine('mysql+pymysql://root:[email protected]/packageing?charset=utf8') engine = create_engine( 'mysql+pymysql://root:[email protected]/gupiao?charset=utf8') realtime = ts.realtime_boxoffice() #realtime.to_sql('realtime_boxoffice',engine) realtime.to_sql('realtime_boxoffice', engine, if_exists='append') day = ts.day_boxoffice() #day.to_sql('day_boxoffice',engine) day.to_sql('day_boxoffice', engine, if_exists='append') month = ts.month_boxoffice() #month.to_sql('month_boxoffice',engine) month.to_sql('month_boxoffice', engine, if_exists='append') cinema = ts.day_cinema() #cinema.to_sql('day_cinema',engine) cinema.to_sql('day_cinema', engine, if_exists='append')
import tushare as ts # 实时票房 # 获取实时电影票房数据,30分钟更新一次票房数据,可随时调用。 df = ts.realtime_boxoffice() print(df) # 每日票房 # 获取单日电影票房数据,默认为上一日的电影票房,可输入参数获取指定日期的票房。 # 似乎只能显示前一日的 df = ts.day_boxoffice("2015-01-01") print(df) # 月度票房 # 获取单月电影票房数据,默认为上一月,可输入月份参数获取指定月度的数据。 df = ts.month_boxoffice('2016-12') print(df) # 影院日度票房 # 获取全国影院单日票房排行数据,默认为上一日,可输入日期参数获取指定日期的数据。 df = ts.day_cinema('2015-12-24') print(df)
def capture_stock_data(): capture_date = datetime.datetime.now().strftime("%Y%m%d") save_dir = "/home/dandelion/stock_data/" + capture_date if not os.path.exists(save_dir): os.mkdir(save_dir) print("The save directory is created successfully!\n", save_dir) print("The save directory is already exist!\n", save_dir) # ======================Daily Command================================================================ # get the boxoffcie data of the last day and save as csvfile named as the capture command ts.day_boxoffice().to_csv( save_dir + "/" + capture_date + "_day_boxoffice.csv", header=True, sep=",", index=False, ) print("day_boxoffice data capture completed!") # get the cinema data of the last day and save as csvfile named as the capture command ts.day_cinema().to_csv( save_dir + "/" + capture_date + "_day_cinema.csv", header=True, sep=",", index=False, ) print("day_cinema data capture completed!") ts.month_boxoffice().to_csv( save_dir + "/" + capture_date + "_month_boxoffice.csv", header=True, sep=",", index=False, ) print("month_boxoffice data capture completed!") ts.realtime_boxoffice().to_csv( save_dir + "/" + capture_date + "_realtime_boxoffice.csv", header=True, sep=",", index=False, ) print("realtime_boxoffice data capture completed!") # get the stock data index of the last day and save as csvfile named as the capture command ts.get_index().to_csv( save_dir + "/" + capture_date + "_get_index.csv", header=True, sep=",", index=False, ) print("get_index data capture completed!") # get the history cpi data and save as csvfile named as the capture command ts.get_cpi().to_csv( save_dir + "/" + capture_date + "_get_cpi.csv", header=True, sep=",", index=False, ) print("get_cpi data capture completed!") # get the history gdp data by month and save as csvfile named as the capture command ts.get_gdp_year().to_csv( save_dir + "/" + capture_date + "_get_gdp_year.csv", header=True, sep=",", index=False, ) print("get_gdp_year data capture completed!") # get today all stock data and save as csvfile named as the capture command # ts.get_today_all().to_csv(save_dir+'/'+capture_date+'_get_today_all.csv',header=True,sep=',',index=False) # get detail information of the top brokers today and save as csvfile named as the capture command ts.broker_tops().to_csv( save_dir + "/" + capture_date + "_broker_tops.csv", header=True, sep=",", index=False, ) print("broker_tops data capture completed!") # get detail information of the top brokers today and save as csvfile named as the capture command ts.cap_tops().to_csv( save_dir + "/" + capture_date + "_cap_tops.csv", header=True, sep=",", index=False, ) print("cap_tops data capture completed!") ts.get_area_classified().to_csv( save_dir + "/" + capture_date + "_get_area_classified.csv", header=True, sep=",", index=False, ) print("get_area_classified data capture completed!") # ts.get_balance_sheet(code='').to_csv(save_dir+'/'+capture_date+'_get_balance_sheet.csv',header=True,sep=',',index=False) # print('get_balance_sheet data capture completed!') # ts.get_cash_flow(code='').to_csv(save_dir+'/'+capture_date+'_get_cash_flow.csv',header=True,sep=',',index=False) # print('get_cash_flow data capture completed!') ts.get_day_all().to_csv( save_dir + "/" + capture_date + "_get_day_all.csv", header=True, sep=",", index=False, ) print("get_day_all data capture completed!") ts.get_cashflow_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_cashflow_data.csv", header=True, sep=",", index=False, ) print("get_cashflow_data data capture completed!") ts.get_concept_classified().to_csv( save_dir + "/" + capture_date + "_get_concept_classified.csv", header=True, sep=",", index=False, ) print("get_concept_classified data capture completed!") ts.get_debtpaying_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_debtpaying_data.csv", header=True, sep=",", index=False, ) print("get_debtpaying_data data capture completed!") ts.get_deposit_rate().to_csv( save_dir + "/" + capture_date + "_get_deposit_rate.csv", header=True, sep=",", index=False, ) print("get_deposit_rate data capture completed!") ts.get_gdp_contrib().to_csv( save_dir + "/" + capture_date + "_get_gdp_contrib.csv", header=True, sep=",", index=False, ) ts.get_gdp_for().to_csv( save_dir + "/" + capture_date + "_get_gdp_for.csv", header=True, sep=",", index=False, ) ts.get_gdp_pull().to_csv( save_dir + "/" + capture_date + "_get_gdp_pull.csv", header=True, sep=",", index=False, ) ts.get_gdp_quarter().to_csv( save_dir + "/" + capture_date + "_get_gdp_quarter.csv", header=True, sep=",", index=False, ) print("get_gdp_ data capture completed!") # ts.get_gdp_year().to_csv(save_dir+'/'+capture_date+'_get_gdp_year.csv',header=True,sep=',',index=False) ts.get_gem_classified().to_csv( save_dir + "/" + capture_date + "_get_gem_classified.csv", header=True, sep=",", index=False, ) ts.get_gold_and_foreign_reserves().to_csv( save_dir + "/" + capture_date + "_get_gold_and_foreign_reserves.csv", header=True, sep=",", index=False, ) ts.get_growth_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_growth_data.csv", header=True, sep=",", index=False, ) ts.get_industry_classified().to_csv( save_dir + "/" + capture_date + "_get_industry_classified.csv", header=True, sep=",", index=False, ) ts.get_hs300s().to_csv( save_dir + "/" + capture_date + "_get_hs300s.csv", header=True, sep=",", index=False, ) ts.get_sz50s().to_csv( save_dir + "/" + capture_date + "_get_sz50s.csv", header=True, sep=",", index=False, ) ts.get_zz500s().to_csv( save_dir + "/" + capture_date + "_get_zz500s.csv", header=True, sep=",", index=False, ) ts.get_operation_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_operation_data.csv", header=True, sep=",", index=False, ) ts.get_stock_basics().to_csv( save_dir + "/" + capture_date + "_get_stock_basics.csv", header=True, sep=",", index=False, ) ts.get_report_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_report_data.csv", header=True, sep=",", index=False, ) ts.inst_detail().to_csv( save_dir + "/" + capture_date + "_inst_detail.csv", header=True, sep=",", index=False, ) ts.inst_tops().to_csv( save_dir + "/" + capture_date + "_inst_tops.csv", header=True, sep=",", index=False, ) print("inst_tops data capture completed!") ts.new_stocks().to_csv( save_dir + "/" + capture_date + "_new_stocks.csv", header=True, sep=",", index=False, ) print("new_stocks data capture completed!") ts.top_list().to_csv( save_dir + "/" + capture_date + "_top_list.csv", header=True, sep=",", index=False, ) print("top_list data capture completed!")
for i in range(aaa.shape[0]): print(aaa.iloc[i,1]) print("aaa result={result}".format(result=aaa[i,1])) df=pd.DataFrame(columns=['month','cpi']) for i in range(aaa.shape[0]): gap=pd.DataFrame({"month":aaa.iloc[i,0],"cpi":(aaa.iloc[i,1].astype(float)-bbb.astype(float))}) df=df.append(gap) #票房数据 piaofang=ts.day_cinema('2017-10-02') pd.set_option pd.set_option('display.max_columns', 200) pd.set_option('display.width', 1000) piaofang[piaofang['price'].max()] rl_piaofang=ts.realtime_boxoffice() last_m_pf=ts.month_boxoffice('2017-07') piaofang_df=pd.DataFrame()
def GetMonthBoxOffice(): df = ts.month_boxoffice() df = df.to_json(force_ascii=False) print(df) return df
#b=[3,6,7,4,8,3,7,3,7] #a = [(1,3,4,5),(2,6,8,5),(4,8,6,9)] import tushare as ts import pandas as pd import matplotlib.pyplot as plt x = [] y = [] for year in range(2013, 2019): for m in range(1, 13): mstr = str(m).zfill(2) ymstr = str(year) + '-' + str(mstr) if ymstr == '2018-02': break df = ts.month_boxoffice(ymstr) sum = 0 for i in range(11): sum += int(df['boxoffice'][i]) x.append(ymstr) y.append(sum) x2 = range(len(x)) plt.plot(x2, y) #plot(x轴数据,y轴数据,展现形式) plt.xticks(x2, x, rotation=45) #plt.figure(figsize=(40,5)) fig = plt.gcf() fig.set_size_inches(18.5, 10.5) plt.show()
# 练习题 # 利用TuShare的电影数据接口,获取电影月票房信息。 # 找到top3的电影,利用接口再次查询它们近7日的票房信息,并绘制成图片。 from matplotlib.font_manager import FontProperties import matplotlib.pyplot as plt import numpy as np import tushare as ts import datetime # 默认是取上月月票房,传参传入本月 today = datetime.date.today() today_display = str(today) month = today_display[:7] movie = ts.month_boxoffice(month) # 如果当月刚开始,没有足够的电影信息,则使用上个月的数据 if len(movie) < 3: movie = ts.month_boxoffice() # 取排行榜前3的电影 top_three = movie.head(3) # 保存top3电影名称 movie_names = list(top_three['MovieName'].values) print(movie_names) # 用于保存获取到的电影的票房信息 boxoffice_record = {} for i in range(3): boxoffice_record[movie_names[i]] = [] print(boxoffice_record)
import tushare as ts data = ts.month_boxoffice() print(data)
# http://tushare.org/boxoffice.html#id4 import json import tushare as ts # df = ts.month_boxoffice() # 取上一月票房数据 df = ts.month_boxoffice('2019-09') # 取2015年10月的数据 df = df[0:5] # print(df) d = {} for idx, row in df.iterrows(): d[idx] = {} d[idx]['Irank'] = eval(row['Irank']) d[idx]['MovieName'] = row['MovieName'] d[idx]['avgboxoffice'] = row['avgboxoffice'] d[idx]['avgshowcount'] = row['avgshowcount'] d[idx]['boxoffice'] = row['boxoffice'] # print(d) with open('hello.txt', 'w') as f: json.dump(d, f) with open('hello.txt', 'r') as f: dd = json.load(f) print(dd)