![image](https://hackmd.io/_uploads/SkJqMpY9p.png) ![image](https://hackmd.io/_uploads/BktIMTYqT.png) ![image](https://hackmd.io/_uploads/SkYCzTYc6.png) ![image](https://hackmd.io/_uploads/SyMv76F5p.png) ![image](https://hackmd.io/_uploads/Hyt3maKqT.png) ![image](https://hackmd.io/_uploads/Sk8W46F9T.png) ![image](https://hackmd.io/_uploads/S138NaYca.png) ![image](https://hackmd.io/_uploads/ByltNTFqT.png) ![image](https://hackmd.io/_uploads/rJNnNat9T.png) ptt-stock股票版 * 利用滑鼠右鍵點選其中一個標題後,再點選「檢查」,即可看到對應的html碼: ![image](https://hackmd.io/_uploads/ryPpd265T.png) 其中標題都是列於div class="title"中 * 取得文章中所有股票的標題 ``` #載入Selenium相關模組 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options #設定Chrome Driver的執行檔路徑,路徑名稱不能有中文 options=Options() options.chrome_executable_path='D:\Teach\Code\chromedriver.exe' #建立Driver物件實體,用程式操作瀏覽器運作 driver=webdriver.Chrome(options=options) driver.get('https://www.ptt.cc/bbs/Stock/index.html') #print(driver.page_source) #取得網頁的原始碼 tags=driver.find_elements(By.CLASS_NAME,"title") #搜尋class 屬性是title的所有標籤 for tag in tags: print(tag.text) driver.close() ``` * 取得上頁的資料 ![image](https://hackmd.io/_uploads/rJ0hnna5a.png) 可模擬按「上頁」的動作: ``` #載入Selenium相關模組 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options #設定Chrome Driver的執行檔路徑,路徑名稱不能有中文 options=Options() options.chrome_executable_path='D:\Teach\Code\chromedriver.exe' #建立Driver物件實體,用程式操作瀏覽器運作 driver=webdriver.Chrome(options=options) driver.get('https://www.ptt.cc/bbs/Stock/index.html') #print(driver.page_source) #取得網頁的原始碼 tags=driver.find_elements(By.CLASS_NAME,"title") #搜尋class 屬性是title的所有標籤 print('1:') for tag in tags: print(tag.text) #取得上頁的文章標題 link=driver.find_element(By.LINK_TEXT,"‹ 上頁") link.click() #模擬使用者點擊上頁的標籤 print('2:') tags=driver.find_elements(By.CLASS_NAME,"title") #搜尋class 屬性是title的所有標籤 for tag in tags: print(tag.text) driver.close() ```