我正在编写 Python 脚本来启动 Chrome 浏览器以及绕过 Cloudfare 机器人检测(验证码、Java 渲染等)。我正在使用以下脚本,但 Chrome 无法启动。
“pip 安装未检测到的 chromedriver
导入未检测到的_chromedriver 作为 uc
def open_webpage(url): # 设置 Chrome 选项 options = uc.ChromeOptions()
# Switch Undetected ChromeDriver to Headless Mode
options.add_argument('--headless') # Correct argument for headless mode
options.add_argument('--user-agent=Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36')
# Proxy settings: Specify your proxy address and port
proxy_address = "socks://username:password@proxyserver:port"
options.add_argument(f'--proxy-server={proxy_address}')
# Create a Chrome browser instance with undetected-chromedriver
driver = uc.Chrome(options=options)
# Fetch the current user agent to verify
current_user_agent = driver.execute_script("return navigator.userAgent;")
print("Current User Agent:", current_user_agent)
# Open the specified URL
driver.get(url)
open_webpage('https://www.indeed.com')
“
首先,使用导入 Web 浏览器
import webbrowser
有多种方法可以做到这一点,但最简单的方法是如果已有浏览器,则此操作将在新选项卡中打开目标页面,否则将打开一个新的标准浏览器窗口。 这是文档,其中包括一些其他方法,它们会产生不同的结果,但这种方法应该适合您。
一个可以工作的程序可能看起来像这样:
也许尝试使用 WebBrowser 模块?
我对 Python 不太熟悉,所以如果这不起作用,我很抱歉。