2021年7月26日 星期一

用Python爬農情報告可以得知南投近十年來在百香果的耕種面積成長快一倍


資料來源:農情報告資源網(https://agr.afa.gov.tw/afa/afa_frame.jsp)

從上圖我們可以看出南投近十年來在百香果的耕種面積成長快一倍。

程式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#使用爬蟲技術取得資料
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_years = ['100', '101', '102', '103', '104', '105', '106', '107', '108', '109']
my_data = {'accountingyear': '109', 'item': '03', 'crop':'643', 'city':'0008'}
my_area = []
for year in my_years:
    my_data['accountingyear'] = year
    r = requests.post("https://agr.afa.gov.tw/afa/pgcropcity.jsp",my_data )
    soup = BeautifulSoup(r.text, 'lxml')
    result = soup.find("div", class_="DivRestTbl")
    dfs = pd.read_html(str(result))
    df = dfs[0]
    my_area.append(df.at[2,1])
#繪製圖表
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(my_years)
bar.add_yaxis("耕種面積", my_area)
bar.render()
#請用瀏覽器打開reder.html檔案即可以觀看

2021年7月25日 星期日

梅生產的前十名鄉鎮有那些?Python一下!

 

程式:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_data = {'accountingyear': '109', 'item': '03', 'crop':'635',
    'city':'0008', 'desc_asc':'desc'}
r = requests.post("https://agr.afa.gov.tw/afa/pgcropmeaqty_all.jsp",my_data )
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find("div", class_="DivRestTbl")
dfs = pd.read_html(str(result))
df = dfs[0]
for item in range(1, 11):
    print("梅種植面積排第{}名的鄉鎮是{}, 佔合全國收量的{}%".format(
        item, df.at[item+2,0], int(df.at[item+2,4])/int(df.at[2,4])*100))

可可生產的前十名鄉鎮有那些?Python一下!

 

可可全國生產最多鄉鎮是南投的名間鄉,佔全國收量20.3%。

程式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_data = {'accountingyear': '109', 'item': '03', 'crop':'647',
    'city':'0008', 'desc_asc':'desc'}
r = requests.post("https://agr.afa.gov.tw/afa/pgcropmeaqty_all.jsp",my_data )
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find("div", class_="DivRestTbl")
dfs = pd.read_html(str(result))
df = dfs[0]
for item in range(1, 11):
    print("可可種植面積排第{}名的鄉鎮是{}, 佔合全國收量的{}%".format(
        item, df.at[item+2,0], int(df.at[item+2,4])/int(df.at[2,4])*100))

香蕉生產的前十名鄉鎮有那些?Python一下!


 程式:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_data = {'accountingyear': '109', 'item': '03', 'crop':'601',
    'city':'0008', 'desc_asc':'desc'}
r = requests.post("https://agr.afa.gov.tw/afa/pgcropmeaqty_all.jsp",my_data )
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find("div", class_="DivRestTbl")
dfs = pd.read_html(str(result))
df = dfs[0]
for item in range(1, 11):
    print("香蕉種植面積排第{}名的鄉鎮是{}, 佔合全國收量的{}%".format(
        item, df.at[item+2,0], int(df.at[item+2,4])/int(df.at[2,4])*100))

節瓜生產的前十名鄉鎮有那些?Python一下!




很難想像,節瓜的重要產地是在南投仁愛鄉,佔全國的60%。

程式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_data = {'accountingyear': '109', 'item': '03', 'crop':'485',
    'city':'0008', 'desc_asc':'desc'}
r = requests.post("https://agr.afa.gov.tw/afa/pgcropmeaqty_all.jsp",my_data )
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find("div", class_="DivRestTbl")
dfs = pd.read_html(str(result))
df = dfs[0]
for item in range(1, 11):
    print("節瓜種植面積排第{}名的鄉鎮是{}, 佔合全國收量的{}%".format(
        item, df.at[item+2,0], int(df.at[item+2,4])/int(df.at[2,4])*100))

茭白筍生產的前十名鄉鎮有那些?Python一下!

 



茭白筍和百香果都是南投埔里鎮很重要的產物,高居全國榜首,佔88.2%,程式:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_data = {'accountingyear': '109', 'item': '03', 'crop':'418',
    'city':'0008', 'desc_asc':'desc'}
r = requests.post("https://agr.afa.gov.tw/afa/pgcropmeaqty_all.jsp",my_data )
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find("div", class_="DivRestTbl")
dfs = pd.read_html(str(result))
df = dfs[0]
for item in range(1, 11):
    print("茭白筍種植面積排第{}名的鄉鎮是{}, 佔合全國收量的{}%".format(
        item, df.at[item+2,0], int(df.at[item+2,4])/int(df.at[2,4])*100))

食用蕃茄生產的前十名鄉鎮有那些?Python一下!

 




程式:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import requests
from bs4 import BeautifulSoup
import pandas as pd
my_data = {'accountingyear': '109', 'item': '03', 'crop':'425',
    'city':'0008', 'desc_asc':'desc'}
r = requests.post("https://agr.afa.gov.tw/afa/pgcropmeaqty_all.jsp",my_data )
soup = BeautifulSoup(r.text, 'lxml')
result = soup.find("div", class_="DivRestTbl")
dfs = pd.read_html(str(result))
df = dfs[0]
for item in range(1, 11):
    print("食用蕃茄收量排第{}名的鄉鎮是{}, 佔合全國收量的{}%".format(
        item, df.at[item+2,0], int(df.at[item+2,4])/int(df.at[2,4])*100))