【Python】使用 Selenium + Firefox 獲取 HTML

# -*- coding: utf-8 -*-
# The tool is based on selenium with proxy.
# Python 3.8.1, selenium 3.141.0, geckodriver v0.26.0, Firefox ESR 68.6
# selenium api: https://www.selenium.dev/selenium/docs/api/py/api.html
# geckodriver: https://github.com/mozilla/geckodriver/releases

from selenium import webdriver

socksPort = 
httpPort = 
sslPort = 
fxBinaryPath = '' # firefox.exe path
geckodriverPath = '' # geckodriver.exe path

fxProfile = webdriver.firefox.firefox_profile.FirefoxProfile()
fxProfile.set_preference('network.proxy.type', 1)
fxProfile.set_preference('network.proxy.socks', '127.0.0.1')
fxProfile.set_preference('network.proxy.socks_port', socksPort)
fxProfile.set_preference('network.proxy.http', '127.0.0.1')
fxProfile.set_preference('network.proxy.http_port', httpPort)
fxProfile.set_preference('network.proxy.ssl', '127.0.0.1')
fxProfile.set_preference('network.proxy.ssl_port', sslPort)
fxProfile.set_preference('network.proxy.socks_remote_dns', True)
fxProfile.set_preference('network.trr.mode', 2)
fxProfile.set_preference('permissions.default.image', 2)
fxDriver = webdriver.firefox.webdriver.WebDriver(firefox_profile=fxProfile, firefox_binary=fxBinaryPath, executable_path=geckodriverPath)

fxDriver.get(url)
html = fxDriver.page_source
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章