接著試驗pandas的繪圖功能,花了我好久的時間,原來要吃圖形的X與Y軸要吃資料形態要吃數字的Series,畫起圖來才會比較順手。
使用了pandas的plot繪製直線圖,程式碼修改如下:
# -*- coding: utf-8 -*- """ Created on Wed Jan 2 13:38:28 2019 @author: ryoliu """ import requests from bs4 import BeautifulSoup import pandas as pd url = 'https://goodinfo.tw/StockInfo/StockBzPerformance.asp?STOCK_ID=1402' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) \ Chrome/65.0.3325.181 Safari/537.36'} r = requests.get(url, headers = headers) r.encoding = 'utf-8' soup = BeautifulSoup(r.text, 'html.parser') rows = soup.find('div', {"id": "divFinDetail"}).find('table', {"class": "solid_1_padding_4_0_tbl"}) dfs = pd.read_html(str(rows)) df=pd.DataFrame(dfs[0]).iloc[3:19,0:3] df.columns= ['years','stockcost','summarize'] df.stockcost=pd.Series(pd.to_numeric(df.stockcost)) df.years=pd.Series(pd.to_numeric(df.years)) df.plot(x='years',y='stockcost', kind='line')
執行結果如下:
沒有留言:
張貼留言