瀏覽器爬蟲入門:Python+Selenium+ChromeDriver


下載對應版本的chromedriver.exe: http://chromedriver.storage.googleapis.com/index.html

下載后將chromedriver.exe放到python安裝目錄或其下的Scripts目錄下,就可以正常調用。

from selenium import webdriver
import os

driver = webdriver.Chrome()
driver.maximize_window() # chrome窗口最大化 driver.get(
'http://www.baidu.com') driver.close() # 關閉driver(關閉當前窗口) os.system('taskkill /f /im chromedriver.exe') # 關閉chromedriver.exe進程 #如果pycharm執行os.system操作產生亂碼: # File > Settings > Editor > File Encodings,將Global Encoding改成GBK即可。

 

from selenium import webdriver#下面3行代碼執行后,不會打開Chrome瀏覽器,不會產生chromedriver.exe進程
options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(chrome_options=options)

driver.get('http://www.baidu.com')
print(driver.title) #獲取打開網址的標題
driver.find_element_by_id('kw').send_keys('博客園') #百度搜索'博客園'
driver.quit() # 關閉driver(關閉窗口並結束chromedriver.exe進程)

driver.close()和driver.quit()的區別:https://blog.csdn.net/huilan_same/article/details/52329804


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM