pyhton selenium爬虫控制台信息输出问题解决


from selenium.webdriver.chrome.options import Options  # 无头浏览器模块
# 创建一个参数对象,用来控制chrome
chrome_options = Options()
# 后面的两个是固定写法 必须这么写 用来控制chrome以无界面方式打开
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# 下面一行用于关闭编译后控制台会显示Devtools的信息
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
path = r'F:\pythonProject1\chromedriver\chromedriver.exe'
# path = r'D:\chromedriver\chromedriver.exe'  # 设置谷歌浏览器驱动路径
self.driver = webdriver.Chrome(executable_path=path, options=chrome_options)  # 打开谷歌浏览器驱动,并将上面配置的控制信息options传入

出现‘WARNING:dns_config_service_win.cc’输出时,给电脑配置静态DNS可以解决,如:8.8.8.8
出现‘INFO:CPNSOLE’输出时,修改F:\python\Lib\site-packages\selenium\webdriver\common\service.py源代码解决,代码如下:
from win32process import CREATE_NO_WINDOW
def start(self):
        """
        Starts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        """
        try:
            cmd = [self.path]
            cmd.extend(self.command_line_args())
            self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE,
                                            creationflags=CREATE_NO_WINDOW)

导入库后,在start方法下的subprocess.Popen()里面添加‘creationflags=CREATE_NO_WINDOW’,可以解决控制输出INFO问题

 

posted @ 2021-08-19 10:01  柳道  阅读(628)  评论(0)    收藏  举报