1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| import requests,json,re,csv from pyecharts import options as opts from pyecharts.charts import Bar
def spyder(): url ="https://api.cntv.cn/olympic/getOlyMedals?serviceId=pcocean&itemcode=GEN-------------------------------&t=jsonp&cb=banomedals" data = requests.get(url=url).text data = re.sub('omedals1\(','',data) data = re.sub('\);','',data) jData = json.loads(data) MedalsList = jData['data']['medalsList'] String = '奥运奖牌榜\n' with open('奖牌榜.csv', 'a', encoding="utf-8", newline='') as f: header = ['排名', '国家', '金牌', '银牌', '铜牌', '总数'] f_csv = csv.writer(f) f_csv.writerow(header)
for i in MedalsList: rank = i['rank'] count = i['count'] gold = i['gold'] silver = i['silver'] bronze = i['bronze'] countryname = i['countryname']
Csvitem = [] Csvitem.append(rank) Csvitem.append(countryname) Csvitem.append(count) Csvitem.append(gold) Csvitem.append(silver) Csvitem.append(bronze) String += '排名:'+ rank + '\t' + countryname + '\t金牌:' + gold + '\t银牌:' + silver + '\t铜牌' + bronze + '\t总数:' + count + '\n'
with open('奖牌榜.csv', 'a', encoding="utf-8", newline='') as f: f_csv = csv.writer(f) f_csv.writerow(Csvitem) return MedalsList
def drow(medalslist): tGold = [] tSilver = [] tBronze = [] tCountryName = []
for i in medalslist: tGold.append(i['gold']) tSilver.append(i['silver']) tBronze.append(i['bronze']) tCountryName.append(i['countryname'])
c = ( Bar(init_opts=opts.InitOpts(width='1440px', height='2000px')) .add_yaxis("金牌", tGold, stack="stack1") .add_yaxis("银牌", tSilver, stack="stack1") .add_yaxis("铜牌", tBronze, stack="stack1") .add_xaxis(tCountryName) .reversal_axis() .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .set_global_opts(title_opts=opts.TitleOpts(title="2020东京奥运奖牌榜"), yaxis_opts=opts.AxisOpts(is_inverse=True) ) ) c.render("MedalList.html") print('ok')
if __name__ == '__main__': drow(spyder())
|