Skip to content

Instantly share code, notes, and snippets.

@ting11222001
Last active June 12, 2020 02:36
Show Gist options
  • Save ting11222001/10678ae3ac94d0b739a470d7d1957d48 to your computer and use it in GitHub Desktop.
Save ting11222001/10678ae3ac94d0b739a470d7d1957d48 to your computer and use it in GitHub Desktop.
#載入這次需要的所有套件和function
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#安裝chrome WebDriver瀏覽器驅動程式
#https://chromedriver.chromium.org/downloads (注意先確認自己的chrome版本)
#下載到自己指定的存放位置,並解壓縮
chrome_driver_path = '填入自己的下載檔的檔案位置'
#跳出虛擬瀏覽器視窗
driver = webdriver.Chrome(chrome_driver_path)
#WebDriver等待10秒才開始
driver.implicitly_wait(10)
#連線到目標網站python.org
url = 'https://www.python.org/'
driver.get(url)
#注意driver.find_element_by_css_selector()裡面是放輸入搜尋欄的selector位置
keyword = driver.find_element_by_css_selector("#id-search-field")
#在搜尋欄打關鍵字list
keyword.send_keys('list')
#按go送出之後才爬取而且網頁會自動click 'go'
driver.find_element_by_css_selector("#submit").click()
#讓python程式暫停5秒
time.sleep(5)
#再擷取list搜尋結果頁面上的文章標題和內容
items = driver.find_elements_by_xpath("//ul[@class='list-recent-events menu']")
print('item=====')
for item in items:
print(item.text)
#完成後關閉WebDriver
driver.quit()
@ting11222001
Copy link
Author

Added on 20200612

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment