option = webdriver.ChromeOptions() option.add_argument("start-maximized") option.add_argument("--disable-blink-features=AutomationControlled") option.add_experimental_option("excludeSwitches", ["enable-automation"]) option.add_experimental_option("useAutomationExtension", False) browser = webdriver.Chrome(options=option) browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{ 'source':'''Object.defineProperty(navigator, 'Cabdriver', { get: () =>false''' }) city = [ '焦作', ] conn = pymysql.connect(host='localhost', user='root', db='weatherman', passwd='12345678', charset='utf8') cursor = conn.cursor()
时间: 2024-02-29 07:54:13 浏览: 25
这段代码是使用 Python 的 Selenium 库通过 Chrome 浏览器模拟用户操作,访问一个网站并获取数据。具体来说,代码中的 `webdriver.ChromeOptions()` 用于配置 Chrome 浏览器的参数,`webdriver.Chrome(options=option)` 创建一个 Chrome 浏览器对象,`browser.execute_cdp_cmd()` 用于向 Chrome DevTools 协议发送命令,这里是为了修改浏览器中 navigator 对象的 Cabdriver 属性,以避免网站检测到使用自动化工具。然后代码连接了一个 MySQL 数据库,用于存储获取到的数据。最后还缺少获取数据的代码,可能是后面的代码被省略了。
相关问题
undetected-chromedriver使用详解
undetected-chromedriver是一个Python库,用于在使用Selenium时避免被检测到。这个库可以让你的Selenium自动化脚本更加稳定和可靠。
下面是使用undetected-chromedriver的详细步骤:
1. 安装undetected-chromedriver库:
```
pip install undetected-chromedriver
```
2. 导入库和Selenium:
```python
from undetected_chromedriver.v2 import Chrome, ChromeOptions
from selenium import webdriver
```
3. 创建ChromeOptions对象,并设置参数,比如禁止弹出窗口、禁止加载图片等:
```python
options = ChromeOptions()
options.add_argument("--disable-notifications")
options.add_argument("--disable-infobars")
options.add_argument("--mute-audio")
options.add_argument("--disable-popup-blocking")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
options.add_argument("--disable-web-security")
options.add_argument("--disable-xss-auditor")
options.add_argument("--disable-webgl")
options.add_argument("--disable-plugins-discovery")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--disable-features=AutomationControlled")
options.add_argument("--disable-features=VizDisplayCompositor")
options.add_argument("--mute-audio")
options.add_argument("--no-sandbox")
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-accelerated-2d-canvas")
options.add_argument("--disable-accelerated-jpeg-decoding")
options.add_argument("--disable-accelerated-mjpeg-decode")
options.add_argument("--disable-accelerated-video-decode")
options.add_argument("--disable-accelerated-video-encode")
options.add_argument("--disable-client-side-phishing-detection")
options.add_argument("--disable-default-apps")
options.add_argument("--disable-extensions-file-access-check")
options.add_argument("--disable-features=site-per-process")
options.add_argument("--disable-hang-monitor")
options.add_argument("--disable-ipc-flooding-protection")
options.add_argument("--disable-print-preview")
options.add_argument("--disable-prompt-on-repost")
options.add_argument("--disable-renderer-backgrounding")
options.add_argument("--disable-sync")
options.add_argument("--disable-translate")
options.add_argument("--disable-voice-input")
options.add_argument("--disable-wake-on-wifi")
options.add_argument("--enable-automation")
options.add_argument("--start-maximized")
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--ignore-ssl-errors")
```
4. 创建Chrome对象,并设置参数:
```python
driver = Chrome(options=options)
```
5. 在代码中使用driver对象进行操作:
```python
driver.get("https://www.google.com")
print(driver.title)
```
以上就是使用undetected-chromedriver的详细步骤。当然,具体的参数设置还需要根据实际情况进行调整,不同的网站可能需要不同的配置。
selenium 守护线程与主线程
根据提供的引用内容,selenium中的线程默认是非守护线程,如果想要在主线程结束的同时终止其他未运行完的线程,可以将线程设置为守护线程。selenium中可以通过设置`webdriver.ChromeOptions()`中的`detach`参数来实现守护线程的设置。当`detach`参数设置为`True`时,ChromeDriver进程将在后台运行,即使Python脚本退出或崩溃,ChromeDriver进程也将继续运行。当`detach`参数设置为`False`时,ChromeDriver进程将在Python脚本退出时终止。
以下是一个示例代码:
```python
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-extensions')
options.add_argument('--disable-infobars')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-features=VizDisplayCompositor')
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument('--start-maximized')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-default-apps')
options.add_argument('--disable-translate')
options.add_argument('--mute-audio')
options.add_argument('--disable-notifications')
options.add_argument('--disable-logging')
options.add_argument('--log-level=3')
options.add_argument('--output=/dev/null')
options.add_argument('--disable-web-security')
options.add_argument('--user-data-dir=/tmp/user-data')
options.add_argument('--data-path=/tmp/data-path')
options.add_argument('--homedir=/tmp')
options.add_argument('--disk-cache-dir=/tmp/cache-dir')
options.add_argument('--disable-extensions-except=/path/to/extension')
options.add_argument('--disable-extensions-file-access-check')
options.add_argument('--remote-debugging-port=9222')
options.add_argument('--remote-debugging-address=0.0.0.0')
options.add_argument('--disable-background-networking')
options.add_argument('--disable-background-timer-throttling')
options.add_argument('--disable-backgrounding-occluded-windows')
options.add_argument('--disable-breakpad')
options.add_argument('--disable-client-side-phishing-detection')
options.add_argument('--disable-component-update')
options.add_argument('--disable-default-apps')
options.add_argument('--disable-domain-reliability')
options.add_argument('--disable-features=AudioServiceOutOfProcess')
options.add_argument('--disable-hang-monitor')
options.add_argument('--disable-ipc-flooding-protection')
options.add_argument('--disable-notifications')
options.add_argument('--disable-renderer-backgrounding')
options.add_argument('--disable-software-rasterizer')
options.add_argument('--disable-sync')
options.add_argument('--disable-translate-new-ux')
options.add_argument('--disable-webgl')
options.add_argument('--disable-web-security')
options.add_argument('--enable-automation')
options.add_argument('--force-color-profile=srgb')
options.add_argument('--metrics-recording-only')
options.add_argument('--no-first-run')
options.add_argument('--password-store=basic')
options.add_argument('--use-mock-keychain')
options.add_argument('--disable-features=RendererCodeIntegrity')
options.add_argument('--disable-features=NetworkService')
options.add_argument('--disable-features=NetworkServiceInProcess')
options.add_argument('--disable-features=VizHitTestSurfaceLayer')
options.add_argument('--disable-features=IsolateOrigins')
options.add_argument('--disable-features=site-per-process')
options.add_argument('--disable-features=HighContrastMode')
options.add_experimental_option('detach', True)
driver = webdriver.Chrome(options=options)
```