def get_day_boxoffice(day=None): if day == None: result = ts.day_boxoffice().to_json() else: try: result = ts.day_boxoffice(day).to_json() except Exception as e: result = json.dumps({ "error": True, "message": "can not get the data, format date as YYYY-M-D" }) 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_day_boxoffice(day=None): if day == None: total = ts.day_boxoffice().to_csv().split() head = [TRANS.get(i) for i in total[0].split(",")] body = [line.split(",") for line in total[1:]] result = {"head": head, "body": body} else: try: total = ts.day_boxoffice(day).to_csv().split() head = [TRANS.get(i) for i in total[0].split(",")] body = [line.split(",") for line in total[1:]] result = {"head": head, "body": body} except Exception as e: result = { "error": True, "message": "can not get the data, format date as YYYY-M-D" } return result
def day_boxoffice(engine, cdate): tbl = "day_boxoffice" tsl.log(tbl + " start...") try: df = ts.day_boxoffice() df['date'] = cdate - datetime.timedelta(days=1) df.to_sql(tbl, engine, if_exists='append') tsl.log(tbl + " done") except BaseException, e: print e tsl.log(tbl + " error")
def get_day_boxoffice(date=None): df = ts.day_boxoffice(date) if df is not None: if date is None: date = get_yesterday() df.insert(0, 'date', date) res = df.to_sql(fun_day_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 today_box_office(): df = ts.day_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['BoxOffice'][i] + '|' + \ df['SumBoxOffice'][i] + '|' + df['MovieDay'][i] + '|\n' f.write(txt) pass f.write('\n\n') pass
def updateDayBoxoffice(con, date: datetime): import share.model.dao.boxoffice.DayBoxoffice as Model dateString = date.strftime("%Y-%m-%d") logging.debug("Updating day boxoffice of {}".format(dateString)) df = ts.day_boxoffice(date=dateString,retry_count=16) res = [] for _, row in df.iterrows(): obj = Model.rowToORM(row, date=date) if obj is not None: res.append(obj) Base.metadata.create_all(con.engine) con.save_all(res) return
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!")
''' import datetime import tushare as ts from matplotlib import pyplot as plt boxOffice = [] movieDay = [] # ['2018-05-22', '2018-05-23', '2018-05-24', '2018-05-25', '2018-05-26', '2018-05-27', '2018-05-28', '2018-05-29', '2018-05-30'] begin = datetime.date(2018,5,22) end = datetime.date(2018,5,30) d = begin delta = datetime.timedelta(days=1) while d <= end: movieDay.append(d.strftime("%Y-%m-%d")) d += delta print(movieDay) for day in movieDay: data = ts.day_boxoffice(day) print(data[:1]) boxOffice.append(data[:1].SumBoxOffice) print(boxOffice) plt.plot(movieDay,boxOffice,'o',movieDay,boxOffice,'-') plt.show() def data2str(n): today = datetime.today() delta = datetime.timedelta(days=n) start = today - delta return start.strftime("%Y-%m-%d")
def test_boxoffice_1(self): bf: pd.DataFrame = ts.realtime_boxoffice() print(bf) bf: pd.DataFrame = ts.day_boxoffice() print(bf)
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)
g_p.index=g_p.month;g_p g_p.plot(); #工业品价格指数 g_p1=g_p[['ppiip','ppi','qm','rmi','pi']].dropna() g_p1.plot(); #生活价格指数 g_p2=g_p[['cg','food','clothing','roeu','dcg']].dropna();g_p2 g_p2.plot(grid=True) ###10.4 电影票房数据的实时分析 #实时票房 #获取实时电影票房数据,30分钟更新一次票房数据,可随时调用。 r_b = ts.realtime_boxoffice() r_b.info() r_b plt.barh(r_b.MovieName,r_b.BoxOffice.astype(float)); plt.pie(r_b.boxPer,labels=r_b.MovieName); #每日票房 d_b = ts.day_boxoffice() #取上一日的数据 d_b #影院日度票房 #获取全国影院单日票房排行数据,默认为上一日,可输入日期参数获取指定日期的数据。 d_c=ts.day_cinema() #取上一日全国影院票房排行数据 d_c.info() d_c[:10] plt.barh(d_c.CinemaName[:10],d_c.Attendance.astype(float)[:10]);
def get_today_all(): """获取单日电影票房数据""" df = ts.day_boxoffice() print df
#!/usr/bin/python # -*- coding: UTF-8 -*- import tushare as ts df_film = ts.realtime_boxoffice() print(df_film) df_film_date = ts.day_boxoffice('2017-05-17') print(df_film_date)
def GetDayBoxOffice(): df = ts.day_boxoffice() df = df.to_json(force_ascii=False) print(df) return df
def reset_tables(): origin_html = ts.day_boxoffice() origin_html = pd_to_bootstraptabels(origin_html) return origin_html
boxoffice_record = {} for i in range(3): boxoffice_record[movie_names[i]] = [] print(boxoffice_record) # 取每部电影最近7天的票房信息 date_record = [] for i in range(7): # 计算7天的日期 current = str(today + datetime.timedelta(days=i) - datetime.timedelta(days=7)) # 写入记录日期用的数据结构 print(current) date_record.append(current) # 获取单日的电影票房信息 single_day_boxoffice = ts.day_boxoffice(current) # 对当日结果的检查,如果top3中有电影在当日没有被查询到,则认为当日没有票房,记为0 # 反之,则将票房数据记录到预设的dict中 for j in range(len(movie_names)): movie_boxoffice = boxoffice_record[movie_names[j]] top3_movie = single_day_boxoffice[single_day_boxoffice['MovieName'] == movie_names[j]] if top3_movie.empty: movie_boxoffice.append(0) else: movie_boxoffice.append(float(top3_movie['BoxOffice'])) # 绘图 # 汇总日期与票房,方便后续绘图方便 days = np.arange(7) boxoffices = np.array(
import datetime import tushare as ts import numpy as np from scipy import signal from matplotlib import pyplot as plt from matplotlib.font_manager import FontProperties ''' 3、请使用tushare模块提供的api并结合numpy、scipy等模块, 获取前一天电影排行数据中上映天数大于7中日平均票价最高的电影, 分析该电影近一个星期的票房及电影票价的走势, 要求分别绘制出票房走势和平均票价走势 ''' today = datetime.date.today() delta = datetime.timedelta(days=1) yesterday = (today - delta).strftime("%Y-%m-%d") data = ts.day_boxoffice(yesterday) movieDay, movieName, moviePrice = data.MovieDay, data.MovieName, data.AvgPrice #获取需要分析的电影的名称 targetMovieName = None tmpMoviePrice = 0 #通过循环,找到上映天数大于7天并且平均票价最高的电影 for name, day, price in zip(movieName, movieDay, moviePrice): if int(day) > 7: if tmpMoviePrice < int(price): tmpMoviePrice = int(price) targetMovieName = name def date2num(n): today = datetime.date.today() delta = datetime.timedelta(days=n)
# -*- coding: utf-8 -*- import tushare as ts ''' 获取单日电影票房数据,默认为上一日的电影票房,可输入参数获取指定日期的票房。 参数说明: date: 日期(YYYY-MM-DD),默认为上一日 返回值说明: AvgPrice 平均票价 AvpPeoPle 场均人次 BoxOffice 单日票房(万) BoxOffice_Up 环比变化 (%) IRank 排名 MovieDay 上映天数 MovieName 影片名 SumBoxOffice 累计票房(万) WomIndex 口碑指数 ''' ## #取上一日的数据 data = ts.day_boxoffice('2018-01-01') print(data) #df = ts.day_boxoffice('2015-12-24') #取指定日期的数据