Skip to content

時序台

單一時間軸

財經資訊

§ Code

import requests
from bs4 import BeautifulSoup

url = ‘https://www.cnyes.com/twstock/ps_historyprice.aspx?no=2330’

headers = {‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36’}

res = requests.get(url, headers=headers)

soup = BeautifulSoup(res.text, ‘html.parser’)

table = soup.find(‘table’, {‘class’:’tb_outline’})

for tr in table.find_all(‘tr’):
for td in tr.find_all(‘td’):
print(td.text)

§ Output

> stdout : [‘日期n’, ‘開盤價n’, ‘最高價n’, ‘最低價n’, ‘收盤價n’, ‘漲跌價差n’, ‘成交量(千股)n’, ‘成交金額(千元)n’, ‘2020/08/14n’, ‘310.00n’, ‘312.00n’, ‘309.00n’, ‘310.50n’, ‘+1.50n’, ‘7,945n’, ‘2,451n’, ‘2020/08/13n’, ‘309.00n’, ‘310.50n’, ‘308.00n’, ‘309.00n…, ‘2020/07/01n’, ‘310.00n’, ‘311.50n’, ‘309.00n’, ‘310.50n’, ‘+0.50n’, ‘7,945n’, ‘2,451n’, ‘2020/06/30n’, ‘309.00n’, ‘310.50n’, ‘308.00n’, ‘309.00n’, ‘+0.00n’, ‘7,945n’, ‘2,451n’, ‘2020/06/29n’, ‘309.00n’, ‘310.50n’, ‘308.00n’]

§ Markdown

2. 自行選擇一個網站,並使用 BeautifulSoup 爬取該網站的資料

§ Code

import requests
from bs4 import BeautifulSoup

url = ‘https://www.ptt.cc/bbs/Gossiping/index.html’

headers = {‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36’}

res = requests.get(url, headers=headers)

soup = BeautifulSoup(res.text, ‘html.parser’)

for div in soup.find_all(‘div’, {‘class’:’title’}):
for a in div.find_all(‘a’):
print(a.text)

§ Output

> stdout : [‘[新聞] 台灣第一次發行綠色債券!政府籌措約1兆元n’, ‘[問卦] 台灣有沒有可能被中國收回來?n’, ‘[新聞] 台灣發行綠色債券!政府籌措約1兆元n’, ‘[新聞] 台灣發行綠色債券!