https://www.youtube.com/watch?v=ff5L6L5MTCw
取得標籤範例:
BY LINK
drive.find_element(By.LINK_TEXT,"要搜尋文字")
BY CLASS
drive.find_element(By.CLASS_NAME,"要搜尋文字")
操作標籤:
取得標籤內部文字:
element=drive.find_element(搜尋欄位,"要搜尋條件")
element.text
取得標籤屬性:
element=drive.find_element(搜尋欄位,"要搜尋條件")
element.get_attribute("屬性名稱")
模擬click:
element=drive.find_element(搜尋欄位,"要搜尋條件")
element.click().
=====================
#爬ppt stock
#pip install selenium
#https://www.ptt.cc/bbs/Stock/index.html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
#chrom driver path https://chromedriver.chromium.org/downloads
options=Options()
options.chrome_executable_path="G:\我的雲端硬碟\PY\練習\chromdriver"
# Driver 物件
driver=webdriver.Chrome(options=options)
#connect ptt
driver.get("https://www.ptt.cc/bbs/Stock/index.html")
#print(driver.page_source) #get html source
tags=driver.find_elements(By.CLASS_NAME,"title")
for tag in tags:
print(tag.text)
#get up page use find_element
link=driver.find_element(By.LINK_TEXT, "‹ 上頁")
link.click()
tags=driver.find_elements(By.CLASS_NAME,"title")
for tag in tags:
print(tag.text)
driver.close()