1. 程式人生 > 程式設計 >Python Selenium實現無視覺化介面過程解析

Python Selenium實現無視覺化介面過程解析

無視覺化介面的意義

有時候我們爬取網頁資料,並不希望看其中的過程,只想看到最後的資料結果就可以了,這時候,***面就很有必要了!

程式碼如下

from selenium import webdriver
from time import sleep
#實現無視覺化介面
from selenium.webdriver.chrome.options import Options
#實現規避檢測
from selenium.webdriver import ChromeOptions

#實現無視覺化介面的操作
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

#實現規避檢測
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])

#如何實現讓selenium規避被檢測到的風險
bro = webdriver.Chrome(executable_path='./chromedriver',chrome_options=chrome_options,options=option)

#無視覺化介面(無頭瀏覽器) phantomJs
bro.get('https://www.baidu.com')

print(bro.page_source)
sleep(2)
bro.quit()

執行效果:

Python Selenium實現無視覺化介面過程解析

打印出網頁程式碼,證明爬取網站資訊成功

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。