iT邦幫忙

1

python爬蟲公開資訊觀測站上的資產負債表

ian 2022-02-17 00:51:572151 瀏覽
  • 分享至 

  • xImage

新手請教各位python大神,我參考了網路上的https://yichungcheng.pixnet.net/blog/post/119887086-python-%E5%AD%B8%E7%BF%92%E4%B9%8B%E8%B7%AF---%E7%88%AC%E8%9F%B2%E5%89%8D%E5%BF%85%E5%82%99%E7%9F%A5%E8%AD%98 這篇文章,
想抓取公開資訊觀測站上的資產負債表,在執行以下程式後現金流量表、損益表都有抓成功,但唯獨資產負債表沒有成功表格不完整,請教該如何修改此程式?

import pandas as pd
from bs4 import BeautifulSoup
import requests

payload={
     'encodeURIComponent': 1,
     'step':1,
     'firstin':1,
     'off':1,
     'isQuery':'Y',
     'TYPEK':'sii',
     'year':109, 
     'season':'03', 
     }
headers = {'Host': 'mops.twse.com.tw','User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'}
url = 'https://mops.twse.com.tw/mops/web/t163sb05'
list_req = requests.post(url, data=payload ,headers=headers)
soup = BeautifulSoup(list_req.content, "html.parser")
stockbroad= soup.find_all('table',{'class': 'hasBorder'})
asset市=pd.read_html(str(stockbroad),header=0)
asset市=pd.concat(asset市, axis=0, ignore_index=True)
print(asset市)

希望得到最後爬蟲的結果像以下損益表的pattern這樣,謝謝!
https://ithelp.ithome.com.tw/upload/images/20220217/201282137cs82YdYwi.png

目前顯示的結果如下圖抓class=_"hasBorder"是只能抓到公司代號、公司名稱等變數,但變數底下的value抓不到(沒有row),用同樣方法抓損益表row和column都有齊全,唯獨資產負債表沒有抓到row的值。
https://ithelp.ithome.com.tw/upload/images/20220218/2012821351d5HY9Eam.png

附上損益表部分如下(跟資產負債表一樣有好幾個table)
https://ithelp.ithome.com.tw/upload/images/20220218/20128213XeEnUKKOZi.png

obarisk iT邦研究生 2 級 ‧ 2022-02-17 23:37:49 檢舉
url: https://mops.twse.com.tw/mops/web/ajax_t163sb05
另外 beautifulsoup 是多餘的. read_html 可以直接把表讀出來
ian iT邦新手 5 級 ‧ 2022-02-18 11:27:41 檢舉
十分感謝你的回答,只是我的問題是用class='hasBorder'抓到的表沒有row,我看原始碼的部分應該是抓得到,只是不知道為什麼結果會是沒有row,後來我用soup.find_all('tr',class_=['even','tblHead'])抓,但沒辦法用.read_html,有點困惑不知道怎麼樣把它變成dataframe
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答