範例一、落花生
1 2 3 4 5 6 7 8 9 10 11 12 13 | import requests import json r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx') text = json.loads(r.text) for row in text: if row['作物名稱'] is None: continue if '落花生' in row['作物名稱']: print ('交易日期:'+row['交易日期']) print ('作物名稱:'+row['作物名稱']) print ('市場名稱:'+row['市場名稱']) print ('平均價:'+str(row['平均價'])) print ('交易量:'+str(row['交易量'])) |
執行結果:
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台北一
平均價:70.0
交易量:144.0
交易日期:114.04.24
作物名稱:落花生-生
市場名稱:板橋區
平均價:35.0
交易量:150.0
交易日期:114.04.24
作物名稱:落花生-生
市場名稱:三重區
平均價:48.4
交易量:950.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台中市
平均價:27.0
交易量:300.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:豐原區
平均價:50.0
交易量:18.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:高雄市
平均價:72.77
交易量:260.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:鳳山區
平均價:27.1
交易量:80.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:屏東市
平均價:20.0
交易量:100.0
交易日期:114.04.23
作物名稱:落花生-熟
市場名稱:台北二
平均價:100.0
交易量:54.0
交易日期:114.04.23
作物名稱:落花生-熟
市場名稱:台北一
平均價:70.0
交易量:134.0
交易日期:114.04.23
作物名稱:落花生-生
市場名稱:板橋區
平均價:35.0
交易量:90.0
交易日期:114.04.23
作物名稱:落花生-生
市場名稱:三重區
平均價:48.4
交易量:950.0
交易日期:114.04.23
作物名稱:落花生-生
市場名稱:台中市
平均價:30.0
交易量:3.0
交易日期:114.04.23
作物名稱:落花生-熟
市場名稱:豐原區
平均價:48.6
交易量:78.0
交易日期:114.04.23
作物名稱:落花生-熟
市場名稱:高雄市
平均價:100.0
交易量:160.0
交易日期:114.04.22
作物名稱:落花生-熟
市場名稱:台北二
平均價:100.0
交易量:60.0
交易日期:114.04.22
作物名稱:落花生-熟
市場名稱:台北一
平均價:71.2
交易量:251.0
交易日期:114.04.22
作物名稱:落花生-生
市場名稱:板橋區
平均價:31.1
交易量:450.0
交易日期:114.04.22
作物名稱:落花生-生
市場名稱:三重區
平均價:48.2
交易量:980.0
交易日期:114.04.22
作物名稱:落花生-熟
市場名稱:台中市
平均價:28.0
交易量:400.0
交易日期:114.04.22
作物名稱:落花生-熟
市場名稱:豐原區
平均價:56.8
交易量:75.2
交易日期:114.04.22
作物名稱:落花生-熟
市場名稱:高雄市
平均價:66.13
交易量:300.0
交易日期:114.04.22
作物名稱:落花生-熟
市場名稱:鳳山區
平均價:29.6
交易量:60.0
交易日期:114.04.22
作物名稱:落花生-生
市場名稱:屏東市
平均價:29.4
交易量:100.0
範例二、指定日期
1 2 3 4 5 6 7 8 9 10 11 12 13 | import requests import json r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx') text = json.loads(r.text) for row in text: if row['作物名稱'] is None: continue if '落花生' in row['作物名稱'] and '114.04.24' in row['交易日期']: print ('交易日期:'+row['交易日期']) print ('作物名稱:'+row['作物名稱']) print ('市場名稱:'+row['市場名稱']) print ('平均價:'+str(row['平均價'])) print ('交易量:'+str(row['交易量'])) |
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台北一
平均價:70.0
交易量:144.0
交易日期:114.04.24
作物名稱:落花生-生
市場名稱:板橋區
平均價:35.0
交易量:150.0
交易日期:114.04.24
作物名稱:落花生-生
市場名稱:三重區
平均價:48.4
交易量:950.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台中市
平均價:27.0
交易量:300.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:豐原區
平均價:50.0
交易量:18.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:高雄市
平均價:72.77
交易量:260.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:鳳山區
平均價:27.1
交易量:80.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:屏東市
平均價:20.0
交易量:100.0
範例三、指定市場
1 2 3 4 5 6 7 8 9 10 11 12 13 | import requests import json r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx') text = json.loads(r.text) for row in text: if row['作物名稱'] is None: continue if '落花生' in row['作物名稱'] and '114.04.24' in row['交易日期'] and'台中市' in row['市場名稱'] : print ('交易日期:'+row['交易日期']) print ('作物名稱:'+row['作物名稱']) print ('市場名稱:'+row['市場名稱']) print ('平均價:'+str(row['平均價'])) print ('交易量:'+str(row['交易量'])) |
執行結果:
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台中市
平均價:27.0
交易量:300.0
範例四、多作物
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import requests import json r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx') crops=['落花生', '玉米-白玉米'] for crop in crops: text = json.loads(r.text) for row in text: if row['作物名稱'] is None: continue if '落花生' in row['作物名稱'] and '114.04.24' in row['交易日期'] and'台中市' in row['市場名稱'] : print ('交易日期:'+row['交易日期']) print ('作物名稱:'+row['作物名稱']) print ('市場名稱:'+row['市場名稱']) print ('平均價:'+str(row['平均價'])) print ('交易量:'+str(row['交易量'])) |
執行結果:
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台中市
平均價:27.0
交易量:300.0
交易日期:114.04.24
作物名稱:落花生-熟
市場名稱:台中市
平均價:27.0
交易量:300.0
沒有留言:
張貼留言