blog-auto-publishing-tools博客自動發佈工具現在已經可以同時支持chrome和firefox了。
很多小夥伴可能對於如何進行配置和啓動不是很瞭解,今天帶給大家一個詳細的保姆教程,只需要跟着我的步驟一步來就可以無障礙啓動了。
前提條件
前提條件當然是先下載 blog-auto-publishing-tools這個博客自動發佈工具,地址如下:https://github.com/ddean2009/blog-auto-publishing-tools
chrome配置
現在的主流瀏覽器肯定是chrome無疑了。所以我們首先聊一聊如何實現對chrome瀏覽器的支持。
使用selenium正常啓動chrome瀏覽器
selenium本身就自帶對chrome的支持,所以在selenium中使用chrome也是一個非常簡單的事情。
-
首先你需要下載安裝chrome。
-
然後使用pip來安裝selenium:
pip install selenium
- 你需要從ChromeDriver下載頁面下載與你的Chrome瀏覽器版本相對應的ChromeDriver。確保你下載的是與你的操作系統和Chrome版本相匹配的版本。
- 然後就可以在代碼中使用了:
from selenium import webdriver
# 指定ChromeDriver的路徑
driver_path = '/path/to/your/chromedriver' # 替換爲你的ChromeDriver路徑
# 創建一個新的Chrome瀏覽器實例
driver = webdriver.Chrome(executable_path=driver_path)
# 訪問一個網站
driver.get('http://www.example.com')
# ... 在這裏執行你的Selenium操作 ...
# 關閉瀏覽器
driver.quit()
簡單是簡單,但是這樣做的缺點是每次都需要下載一個新的,內置的chrome瀏覽器,所以在國內的網絡環境下,很多人以爲是程序被卡死了。
實際上並不是,只是網絡慢而已。
事實上,我們已經已經在使用chrome了,爲什麼不能attach到現有的chrome上運行selenium呢?
使用selenium連接到現有的chrome瀏覽器
這是blog-auto-publishing-tools正在做事情。
優點: 這樣做的優點有兩個。
第一就是不需要重新下載chrome瀏覽器。
第二就是可以藉助現有瀏覽器的登錄態,不需要每次都在程序中重新登錄。因爲有些網站的登錄條件是很複雜的。比如騰訊雲,你需要時不時的進行手機掃碼才能登錄。
我們的步驟如下:
- 下載並安裝 Chrome。
- 下載chrome Driver Chrome Driver。
- chrome 以debug模式啓動
如果是mac電腦,那麼可以先給chrome設置一個alias
alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
以debug模式啓動
chrome --remote-debugging-port=9222
!!!! 注意!!! chrome啓動之後,一定要新開一個空白tab頁,或者隨便打開一個網站,否則後面的selenium可能會出現假死的情況
在命令行你會看到類似下面的內容:
DevTools listening on ws://127.0.0.1:9222/devtools/browser/d4d05dd2-5b74-4380-b02d-12baa123445
這行ws很重要,我們把它記下來。
如果你是windows,那麼在chrome的快捷方式後面加上 --remote-debugging-port=9222 參數。
啓動chrome,輸入chrome://version 檢測 --remote-debugging-port=9222 是否出現在頁面上。
然後輸入:
獲得 webSocketDebuggerUrl:
- 修改配置文件
修改config/common.yaml 裏面的內容:
# chrome driver地址
service_location: /Users/wayne/Downloads/work/chromedriver-mac-arm64/chromedriver
# chrome調試地址
debugger_address: localhost:9222/devtools/browser/4aab2b8b-112c-48a3-ba38-12baa123445
把service_location和debugger_address修改爲你自己本機的配置。
你也可以使用簡單版本的 debugger_address: localhost:9222
我在windows環境下測試過是可以連接的。
在mac環境下有時可以連接,有時候不能連接,大家可以自行測試。
這樣,使用下面的代碼就可以連接到現有的chrome了:
# 啓動瀏覽器驅動服務
service = selenium.webdriver.chrome.service.Service(common_config['service_location'])
# Chrome 的調試地址
debugger_address = common_config['debugger_address']
# 創建Chrome選項,重用現有的瀏覽器實例
options = selenium.webdriver.chrome.options.Options()
options.page_load_strategy = 'normal' # 設置頁面加載策略爲'normal' 默認值, 等待所有資源下載,
options.add_experimental_option('debuggerAddress', debugger_address)
# 使用服務和選項初始化WebDriver
driver = webdriver.Chrome(service=service, options=options)
firefox配置
除了chrome之外,用的最多的應該就是firefox了。
所以blog-auto-publishing-tools也提供了對firefox的支持。
使用selenium正常啓動firefox瀏覽器
要常規使用firefox,你需要下面幾部:
-
下載並安裝 Firefox。
-
下載geckodriver 驅動.下載與你的Firefox瀏覽器版本相對應的geckodriver。確保你下載的是與你的操作系統和Firefox版本相匹配的版本。
-
在代碼中使用:
from selenium import webdriver
# 指定geckodriver的路徑
driver_path = '/path/to/your/geckodriver' # 替換爲你的geckodriver路徑
# 創建一個新的Firefox瀏覽器實例
driver = webdriver.Firefox(executable_path=driver_path)
# 訪問一個網站
driver.get('http://www.example.com')
# ... 在這裏執行你的Selenium操作 ...
# 關閉瀏覽器
driver.quit()
同樣的,因爲網絡問題,所以這種方式在國內的環境中會很慢。
我們看看另外一種方式。
使用selenium連接到現有的firefox瀏覽器
-
下載並安裝 Firefox。
-
下載geckodriver 驅動.下載與你的Firefox瀏覽器版本相對應的geckodriver。確保你下載的是與你的操作系統和Firefox版本相匹配的版本。
-
在firefox的啓動命令之後加上:
-marionette -start-debugger-server 2828
注意,這裏的端口一定要是2828,不能自定義。
這時候你如果打開firefox,就會看到導航欄變成了紅色,表示你已經啓動了遠程調試模式。
輸入about:config
可以看到marionette.port的端口就是2828。
- 修改配置文件
修改config/common.yaml 裏面的內容:
# firefox driver地址
service_location: "D:\\downloads\\geckodriver-v0.34.0-win32\\geckodriver.exe"
把driver_type修改爲firefox。
#driver_type: "chrome"
driver_type: "firefox"
接下來就可以在程序中使用了:
# 啓動瀏覽器驅動服務
service = selenium.webdriver.firefox.service.Service(common_config['service_location'],
service_args=['--marionette-port', '2828',
'--connect-existing'])
# 創建firefox選項,重用現有的瀏覽器實例
options = selenium.webdriver.firefox.options.Options()
options.page_load_strategy = 'normal' # 設置頁面加載策略爲'normal' 默認值, 等待所有資源下載,
driver = webdriver.Firefox(service=service, options=options)
總結
我在mac上測試了chrome,在windows上同時測試了chrome和firefox。都是可以用的。大家有問題可以私信我。