上圖的用十行Python指令,就可以得南投各鄉鎮百香果的生產情形,程式碼如下:
1 2 3 4 5 6 7 8 9 10 | import requests from bs4 import BeautifulSoup import pandas as pd my_data = {'accountingyear': '109', 'item': '03', 'crop':'643', 'city':'0008'} r = requests.post("https://agr.afa.gov.tw/afa/pgcroptown.jsp",my_data ) soup = BeautifulSoup(r.text, 'lxml') result = soup.find("div", class_="DivRestTbl") dfs = pd.read_html(str(result)) df = dfs[0] print(df) |
瞭解埔里鎮佔全南投縣的佔比是?
程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 | import requests from bs4 import BeautifulSoup import pandas as pd my_data = {'accountingyear': '109', 'item': '03', 'crop':'643', 'city':'0008'} r = requests.post("https://agr.afa.gov.tw/afa/pgcroptown.jsp",my_data ) soup = BeautifulSoup(r.text, 'lxml') result = soup.find("div", class_="DivRestTbl") dfs = pd.read_html(str(result)) df = dfs[0] print('埔里鎮百香果的收量是', df.at[9,4], df.at[1,4]) print('南投百香果的收量是', df.at[15,4], df.at[1,4]) print('埔里百香果收量佔南投縣的', int(df.at[9,4])/int(df.at[15,4])*100, '%') |
沒有留言:
張貼留言