# python爬蟲-大奇蹟日 #### 會用到requests 跟 美麗的湯(BeautifulSoup) #### 先使用pip install requests, pip install BeautifulSoup import requests from bs4 import BeautifulSoup #### 今天是大奇蹟日,所以我們要爬台股的頂梁柱: 半導體類股[](https://)https://www.cnyes.com/twstock/index2real.aspx?stockType=T&groupId=24&stitle=%e5%8d%8a%e5%b0%8e%e9%ab%94 #### 透過requests.get前往該網頁,由於要把我們假裝成真的使用者,在headers裡添加一些混淆視聽的 "User-Agent" response = requests.get( "https://www.cnyes.com/twstock/index2real.aspx?stockType=T&groupId=24&stitle=%e5%8d%8a%e5%b0%8e%e9%ab%94", headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36" }) #### 以Beautiful Soup 解析 HTML 程式碼 soup = BeautifulSoup(response.text, "html.parser") data = soup.select("table tr td") #### 在網頁畫面開啟開發人員工具(F12),可得知我們要爬的資料,在html的標籤裡的table > tr > td #### data = soup.select("table tr td") 選取到我們要的欄位  #### print(data)出來看看 [<td>13:30:00</td>, <td><a href="/twstock/profile/2302.htm">2302</a></td>, <td>.....] #### 設a為空字串,對data做for迴圈,由於每11筆會得到完整一檔股票的資料,增加以下判斷式,利用字串串接,最後print出來 a = '' for i in range(len(data)): if (i+1) % 11 == 0: a = a + data[i].text + ' ' print(a) a = '' else: a = a + data[i].text + ' ' #### 結果如下  #### 完整程式碼 import requests from bs4 import BeautifulSoup response = requests.get( "https://www.cnyes.com/twstock/index2real.aspx?stockType=T&groupId=24&stitle=%e5%8d%8a%e5%b0%8e%e9%ab%94", headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36" }) soup = BeautifulSoup(response.text, "html.parser") data = soup.select("table tr td") a = '' for i in range(len(data)): if (i+1) % 11 == 0: a = a + data[i].text + ' ' # if a.split(' ')[1] == '2330': print(a) a = '' else: a = a + data[i].text + ' ' #### 只想看台股不死鳥台積電,可將# if a.split(' ')[1] == '2330'註解打開
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up