         ptt-stock股票版 * 利用滑鼠右鍵點選其中一個標題後,再點選「檢查」,即可看到對應的html碼:  其中標題都是列於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() ``` * 取得上頁的資料  可模擬按「上頁」的動作: ``` #載入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() ```
×
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