如何在 python 中抑制 console/cmd 错误消息
How do I suppress console/cmd error messages in python
如何在 python 中抑制 chromedriver 和 pyinstaller exe 的错误消息?
我注意到当我在 pyinstaller 和 运行 中使用 chromedriver 时,我得到了一个错误消息列表。我一直在尝试删除它们,但到目前为止还没有成功。
更新:似乎无头标志引起了很多悲伤。
DevTools listening on ws://127.0.0.1:12386/devtools/browser/74743659-2c28-45fb-bab4-3836bb91f383
[1213/214223.999:ERROR:service_manager.cc(157)] Connection InterfaceProviderSpec prevented service: content_renderer from binding interface: blink::mojom::ReportingServiceProxy exposed by: content_browser
Improvement, now how to remove this in headless.
Error messages:
DevTools listening on ws://127.0.0.1:12386/devtools/browser/74743659-2c28-45fb-bab4-3836bb91f383
[1213/214223.999:ERROR:service_manager.cc(157)] Connection InterfaceProviderSpec prevented service: content_renderer from binding interface: blink::mojom::ReportingServiceProxy exposed by: content_browser
[1213/214223.999:INFO:CONSOLE(4)] "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", source: http://centrebet.com/Scripts/jquery-1.6.4.min.js (4)
[1213/214226.896:INFO:CONSOLE(42)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/8320572 (42)
[1213/214226.899:INFO:CONSOLE(60)] "Uncaught ReferenceError: bs_auto_expand is not defined", source: http://centrebet.com/Sports/8320572 (60)
[1213/214226.900:INFO:CONSOLE(65)] "Uncaught ReferenceError: gaPush is not defined", source: http://centrebet.com/Sports/8320572 (65)
[1213/214227.987:INFO:CONSOLE(112)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (112)
[1213/214227.988:INFO:CONSOLE(174)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (174)
[1213/214227.989:INFO:CONSOLE(211)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (211)
[1213/214227.991:INFO:CONSOLE(431)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (431)
[1213/214227.991:INFO:CONSOLE(468)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (468)
[1213/214227.998:INFO:CONSOLE(688)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (688)
[1213/214227.998:INFO:CONSOLE(725)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (725)
[1213/214228.001:INFO:CONSOLE(945)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (945)
[1213/214228.001:INFO:CONSOLE(982)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (982)
[1213/214228.002:INFO:CONSOLE(1202)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1202)
[1213/214228.003:INFO:CONSOLE(1239)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (1239)
[1213/214228.004:INFO:CONSOLE(1459)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1459)
[1213/214228.004:INFO:CONSOLE(1496)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (1496)
[1213/214228.005:INFO:CONSOLE(1716)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1716)
[1213/214228.005:INFO:CONSOLE(1753)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (1753)
[1213/214228.006:INFO:CONSOLE(1973)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1973)
代码:
options = Options()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
url = "http://centrebet.com/"
driver.get(url)
def page_counter():
for x in range(1000):
yield x
count = page_counter()
driver.get(url)
sports = driver.find_element_by_id("accordionMenu1_ulSports")
links = [url + link.get_attribute("onclick").replace("menulink('", "").replace("')", "") for link in sports.find_elements_by_xpath('//ul[@id="accordionMenu1_ulSports"]//li//ul//li//ul//li//a[starts-with(@onclick, "menulink")]')]
links = dict((next(count) + 1, e) for e in links)
desc_links = collections.OrderedDict(sorted(links.items(), reverse=True))
for key, value in desc_links.items():
driver.get(value)
我尝试过的:
logging.disable(logging.CRITICAL)
logging.Logger.info = lambda *args, **kwargs: None
sys.stderr = os.devnull
logging.getLogger("requests").setLevel(logging.WARNING)
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
错误消息似乎在循环。也许重写可以修复...
question now is how to remove error messages and suppress messages in
relation to chrome driver
要关闭 chromedriver 日志记录,请使用 DesiredCapabilities
options = Options()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
dc = DesiredCapabilities.CHROME
dc['loggingPrefs'] = {'driver': 'OFF', 'server': 'OFF', 'browser': 'OFF'}
driver = webdriver.Chrome(chrome_options=options, desired_capabilities=dc)
请看这里
这些日志是由 Chrome 发布的。您可以通过启动 Chrome 并将日志级别设置为致命来禁用它们:
options = Options()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
options.add_argument("--log-level=3") # fatal
driver = webdriver.Chrome(chrome_options=options)
尽管有些消息未被此标志过滤,例如 DevTools listening on ...
。
为了避免它们,您必须用 close_fds=True
覆盖 selenium.webdriver.common.service.Service and call subprocess.Popen
以避免文件描述符的继承。
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=True,
stdout=None,
stderr=None,
stdin=None)
我遇到了类似的问题,以下是我的解决方案。
我在 python 脚本中使用 selenium 模块来打开 chromedriver.exe
并自动执行一些在线下载。
我运行脚本使用的是BAT文件。
本来如果我 运行 BAT 文件, chrome 被打开并下载了文件,但是命令行挂在日志语句上
DevTools listening on ...
我解决这个问题的方法是在 bat 文件中添加行
taskkill /im chromedriver.exe /f
所以我的 BAT 文件看起来像这样
python python_script.py
taskkill /im chromedriver.exe /f
日志语句仍然出现,但杀死 chromedriver.exe
似乎结束了 运行。
试试这个:-
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches',['enable-logging']);
希望对您有所帮助。
如何在 python 中抑制 chromedriver 和 pyinstaller exe 的错误消息?
我注意到当我在 pyinstaller 和 运行 中使用 chromedriver 时,我得到了一个错误消息列表。我一直在尝试删除它们,但到目前为止还没有成功。 更新:似乎无头标志引起了很多悲伤。
DevTools listening on ws://127.0.0.1:12386/devtools/browser/74743659-2c28-45fb-bab4-3836bb91f383
[1213/214223.999:ERROR:service_manager.cc(157)] Connection InterfaceProviderSpec prevented service: content_renderer from binding interface: blink::mojom::ReportingServiceProxy exposed by: content_browser
Improvement, now how to remove this in headless.
Error messages:
DevTools listening on ws://127.0.0.1:12386/devtools/browser/74743659-2c28-45fb-bab4-3836bb91f383
[1213/214223.999:ERROR:service_manager.cc(157)] Connection InterfaceProviderSpec prevented service: content_renderer from binding interface: blink::mojom::ReportingServiceProxy exposed by: content_browser
[1213/214223.999:INFO:CONSOLE(4)] "Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.", source: http://centrebet.com/Scripts/jquery-1.6.4.min.js (4)
[1213/214226.896:INFO:CONSOLE(42)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/8320572 (42)
[1213/214226.899:INFO:CONSOLE(60)] "Uncaught ReferenceError: bs_auto_expand is not defined", source: http://centrebet.com/Sports/8320572 (60)
[1213/214226.900:INFO:CONSOLE(65)] "Uncaught ReferenceError: gaPush is not defined", source: http://centrebet.com/Sports/8320572 (65)
[1213/214227.987:INFO:CONSOLE(112)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (112)
[1213/214227.988:INFO:CONSOLE(174)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (174)
[1213/214227.989:INFO:CONSOLE(211)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (211)
[1213/214227.991:INFO:CONSOLE(431)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (431)
[1213/214227.991:INFO:CONSOLE(468)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (468)
[1213/214227.998:INFO:CONSOLE(688)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (688)
[1213/214227.998:INFO:CONSOLE(725)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (725)
[1213/214228.001:INFO:CONSOLE(945)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (945)
[1213/214228.001:INFO:CONSOLE(982)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (982)
[1213/214228.002:INFO:CONSOLE(1202)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1202)
[1213/214228.003:INFO:CONSOLE(1239)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (1239)
[1213/214228.004:INFO:CONSOLE(1459)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1459)
[1213/214228.004:INFO:CONSOLE(1496)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (1496)
[1213/214228.005:INFO:CONSOLE(1716)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1716)
[1213/214228.005:INFO:CONSOLE(1753)] "Uncaught ReferenceError: $ is not defined", source: http://centrebet.com/Sports/71973868 (1753)
[1213/214228.006:INFO:CONSOLE(1973)] "Uncaught ReferenceError: addStaticBubble is not defined", source: http://centrebet.com/Sports/71973868 (1973)
代码:
options = Options()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=options)
url = "http://centrebet.com/"
driver.get(url)
def page_counter():
for x in range(1000):
yield x
count = page_counter()
driver.get(url)
sports = driver.find_element_by_id("accordionMenu1_ulSports")
links = [url + link.get_attribute("onclick").replace("menulink('", "").replace("')", "") for link in sports.find_elements_by_xpath('//ul[@id="accordionMenu1_ulSports"]//li//ul//li//ul//li//a[starts-with(@onclick, "menulink")]')]
links = dict((next(count) + 1, e) for e in links)
desc_links = collections.OrderedDict(sorted(links.items(), reverse=True))
for key, value in desc_links.items():
driver.get(value)
我尝试过的:
logging.disable(logging.CRITICAL)
logging.Logger.info = lambda *args, **kwargs: None
sys.stderr = os.devnull
logging.getLogger("requests").setLevel(logging.WARNING)
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
finally:
sys.stdout = old_stdout
import warnings
def fxn():
warnings.warn("deprecated", DeprecationWarning)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn()
错误消息似乎在循环。也许重写可以修复...
question now is how to remove error messages and suppress messages in relation to chrome driver
要关闭 chromedriver 日志记录,请使用 DesiredCapabilities
options = Options()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
dc = DesiredCapabilities.CHROME
dc['loggingPrefs'] = {'driver': 'OFF', 'server': 'OFF', 'browser': 'OFF'}
driver = webdriver.Chrome(chrome_options=options, desired_capabilities=dc)
请看这里
这些日志是由 Chrome 发布的。您可以通过启动 Chrome 并将日志级别设置为致命来禁用它们:
options = Options()
options.add_argument('--headless')
options.add_argument('--hide-scrollbars')
options.add_argument('--disable-gpu')
options.add_argument("--log-level=3") # fatal
driver = webdriver.Chrome(chrome_options=options)
尽管有些消息未被此标志过滤,例如 DevTools listening on ...
。
为了避免它们,您必须用 close_fds=True
覆盖 selenium.webdriver.common.service.Service and call subprocess.Popen
以避免文件描述符的继承。
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=True,
stdout=None,
stderr=None,
stdin=None)
我遇到了类似的问题,以下是我的解决方案。
我在 python 脚本中使用 selenium 模块来打开 chromedriver.exe
并自动执行一些在线下载。
我运行脚本使用的是BAT文件。
本来如果我 运行 BAT 文件, chrome 被打开并下载了文件,但是命令行挂在日志语句上
DevTools listening on ...
我解决这个问题的方法是在 bat 文件中添加行
taskkill /im chromedriver.exe /f
所以我的 BAT 文件看起来像这样
python python_script.py
taskkill /im chromedriver.exe /f
日志语句仍然出现,但杀死 chromedriver.exe
似乎结束了 运行。
试试这个:-
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches',['enable-logging']);
希望对您有所帮助。