本篇文章從農情報告資源網中,使用Python結合爬蟲和製表的工具,來觀察近十年南投百香果產業的發展情形。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #使用爬蟲技術取得資料 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 = [] my_yield = [] 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]) my_yield.append(df.at[2,4]) #繪製圖表 from pyecharts import Bar, Line, Overlap bar = Bar() bar.add("百香果耕種面積", my_years, my_area) line = Line() line.add("百香果收量", my_years, my_yield) overlap = Overlap() overlap.add(bar) overlap.add(line, is_add_yaxis=True, yaxis_index=1) overlap.render() #請用瀏覽器打開reder.html檔案即可以觀看 |
沒有留言:
張貼留言