Python 无头铬合金驱动器不适用于硒

Python 无头铬合金驱动器不适用于硒,python,selenium,web-scraping,selenium-chromedriver,cloudflare,google-chrome-headless,Python,Selenium,Web Scraping,Selenium Chromedriver,Cloudflare,Google Chrome Headless,当我设置选项时,我当前的刮板有问题。添加参数(“--headless”)。然而,当它被移除时,它工作得非常好。有谁能告诉我如何用无头模式达到同样的效果 下面是我的python代码: from seleniumwire import webdriver as wireDriver from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys

当我设置
选项时,我当前的刮板有问题。添加参数(“--headless”)
。然而,当它被移除时,它工作得非常好。有谁能告诉我如何用无头模式达到同样的效果

下面是我的python代码:

from seleniumwire import webdriver as wireDriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
    
chromedriverPath = '/Users/applepie/Desktop/chromedrivermac'

    def scraper(search):

    mit = "https://orbit-kb.mit.edu/hc/en-us/search?utf8=✓&query="  # Empty search on mit site
    mit += "+".join(search) + "&commit=Search"
    results = []

    options = Options()
    options.add_argument("--headless")
    options.add_argument("--window-size=1440, 900")
    driver = webdriver.Chrome(options=options, executable_path= chromedriverPath)

    driver.get(mit)
    # Wait 20 seconds for page to load
    timeout = 20
    try:
        WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, "header")))
        search_results = driver.find_element_by_class_name("search-results")
        for result in search_results.find_elements_by_class_name("search-result"):
            resultObject = {
                "url": result.find_element_by_class_name('search-result-link').get_attribute("href")
            }
            results.append(resultObject)
        driver.quit()
    except TimeoutException:
        print("Timed out waiting for page to load")
        driver.quit()

    return results
下面是我在
get()之后打印(driver.page\u source)
的屏幕截图:

此屏幕截图

…意味着已将您对网站的请求检测为自动机器人,并随后拒绝您访问该应用程序


解决方案 在这些情况下,一个潜在的解决方案是使用In模式初始化浏览上下文

是一个优化的Selenium Chromedriver修补程序,它不会触发反机器人服务,如蒸馏网络/Imperva/DataDome/Botprotect.io。它会自动下载驱动程序二进制文件并对其进行修补

  • 代码块:

    import undetected_chromedriver as uc
    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.headless = True
    driver = uc.Chrome(options=options)
    driver.get(url)
    

工具书类 您可以在以下内容中找到一些相关的详细讨论:


你能解释一下问题到底是什么吗?@PApostol,当我添加
选项时,刮板没有返回任何结果。添加参数(“--headless”)
。但是,如果删除了
options.add_参数(“--headless”)
,则可以尝试使用
options.headless=True
而不是
options.add_参数(“--headless”)
,以查看是否存在差异。也可以考虑包括一个可以运行的例子来复制这个问题。@ PApostol <代码> opth.HeaveLe= Trase< /Cord>不起作用。@ Apple PosiPee在屏幕上输入一个截图或打印<代码>驱动程序。成功。我收到以下错误:
ssl.SSLCertVerificationError:[ssl:CERTIFICATE\u VERIFY\u FAILED]CERTIFICATE VERIFY FAILED:无法获取本地颁发者证书(\u ssl.c:1122)
@ApplePie
SSLCertVerificationError
是一个证书问题,与通过
headless
模式访问应用程序的一般问题相比,它是一个更为精细的问题。你能根据你的新要求提出一个新问题吗?Stackoverflow贡献者将很乐意帮助您。