1.打开python3X的IDLE,ctrl+N新建一个文件
2.把以下代码复制粘贴后,ctrl+shift+S另存为'*.py',运行即可
import?urllib.request
import?json
import?openpyxl
urls=['USA','CHN','JPN','GBR','ROC']
wk?=?openpyxl.Workbook()
sheet?=?wk.create_sheet()
sheet.append(['startdatecn','subitemname','subitemcode','playid','itemcodename','medaltype','countryname','itemcode','medal','playname','countryid'])
for?i?in?urls:
????url='https://api.cntv.cn/Olympic/getOlyMedalList?t=jsonp&cb=OM&serviceId=pcocean&countryid='+i
????resp?=?urllib.request.urlopen(url).read()
????#print(type(resp))
????medlistdata?=?json.loads(resp[3:-2])
????medlists?=?medlistdata.get('data')['medalList']
????#print(type(medlists))
????for?j?in?medlists:
????????#outputlist?=?j.values()
????????outputlist?=?[j['startdatecn'],j['subitemname'],j['subitemcode'],j['playid'],j['itemcodename'],j['medaltype'],j['countryname'],j['itemcode'],j['medal'],j['playname'],j['countryid']]
????????#print(outputlist)
????????sheet.append(outputlist)
wk.save('C:/Users/wuyaohua/Desktop/getOlyMedalList.xlsx')
print("获取完成!")
|