程式:
1 2 3 4 5 6 7 8 9 | import requests from bs4 import BeautifulSoup import pandas as pd my_data = {'accountingyear': '109', 'item': '03', 'crop':'643', 'city':'00'} 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)) print(dfs) |
計算南投百香果的收量佔全國多少比率?
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':'00'} 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] print('南投縣百香果的收量是', df.at[9,4], df.at[1,4]) print('全國百香果的收量是', df.at[21,4], df.at[1,4]) print('南投百香果收量佔全的', int(df.at[9,4])/int(df.at[21,4])*100, '%') |
執行結果:
沒有留言:
張貼留言