selenium+headless+proxy 的出错

之前的代码

chromedriver_path = "存放webdriver的路径"

options = Options()
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument("disable-infobars")
options.add_argument("--disable-notifications")
options.add_argument('headless')
# 设置代理
options.add_argument("--proxy-server=http://" + ip_port)

driver = webdriver.Chrome(options=options, executable_path=chromedriver_path)

这种方式会得不到数据,的到的页面信息是这样的。



解决方案1

参考文献: https://cloud.tencent.com/developer/article/1567061

         options = webdriver.ChromeOptions()
        # 设置代理
        desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
        desired_capabilities['proxy'] = {
            "httpProxy": "http://121.9.199.31:26605",
            # "ftpProxy": self.proxy_ip,  # 代理ip是否支持这个协议
            # "sslProxy": self.proxy_ip,  # 代理ip是否支持这个协议
            "noProxy": None,
            "proxyType": "MANUAL",
            "class": "org.openqa.selenium.Proxy",
            "autodetect": False
        }

        # 使用无头模式
        options.add_argument('--disable-gpu')
        options.add_argument('--no-sandbox')
        options.add_argument("disable-infobars")
        options.add_argument("--disable-notifications")
        options.add_argument('headless')
        options.add_experimental_option("useAutomationExtension", False)
        options.add_experimental_option("excludeSwitches", ["enable-automation"])

        driver = webdriver.Chrome(chrome_options=options,
                                   executable_path=r'C:\chromedriver.exe',
                                   desired_capabilities=desired_capabilities)



解决方案2

参考文献 :https://cloud.tencent.com/developer/article/1651263

这种方式没有去现实。先记录下

posted @ 2020-09-23 10:02  西半球  阅读(387)  评论(0编辑  收藏  举报