python - 无法强制脚本不在控制台中打印错误日志详细信息

标签 python python-3.x selenium web-scraping seleniumwire

我正在尝试使用 python 结合 seleniumwire 创建一个脚本在其中实现 poxies。该脚本有时可以正常工作,但大多数时候即使 status_code 为 200,也会显示错误日志详细信息。我希望删除这些日志详细信息。脚本中硬编码的 IP 地址取自免费代理站点,因此目前可能没有任何用处。

这就是我正在尝试的:

from seleniumwire import webdriver

URL = 'https://www.zillow.com/Houston,-TX/houses/'

options = {
    'mitm_http2': False,
    'proxy': {'https': f'https://136.226.33.115:80'}
}

driver = webdriver.Chrome(seleniumwire_options=options)

driver.get(URL)
assert driver.requests[0].response.status_code==200
post_links = [i.get_attribute("href") for i in driver.find_elements_by_css_selector("article[role='presentation'] > .list-card-info > a.list-card-link")]
for individual_link in post_links:
    driver.get(individual_link)
    assert driver.requests[0].response.status_code==200
    post_title = driver.find_element_by_css_selector("h1").text
    print(post_title)
driver.quit()

这是我可以在控制台中看到的错误日志详细信息类型:

127.0.0.1:55825: request
  -> HTTP protocol error in client request: Server disconnected
127.0.0.1:55967: request
  -> HTTP protocol error in client request: Server disconnected
127.0.0.1:64891: request
  -> HTTP protocol error in client request: Server disconnected
127.0.0.1:61466: request
  -> HTTP protocol error in client request: Server disconnected
127.0.0.1:51332: request
  -> HTTP protocol error in client request: Server disconnected
127.0.0.1:52783: request
  -> HTTP protocol error in client request: Server disconnected

How can I force the script not to print those log details?

最佳答案

您可以通过以下代码行定义所需的日志错误级别:

options .add_argument('--log-level=3')

将此添加到您的选项中。

log-level 属性设置最低日志级别。
有效值为 0 到 3:

信息=0,
警告=1,
LOG_ERROR = 2,
LOG_FATAL = 3。

默认为 0。

关于python - 无法强制脚本不在控制台中打印错误日志详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67970068/

相关文章:

python - Frozen Flask 创建文件但链接错误

python - 像 Optimizely 这样的网络代理

python - Twine 在不提示输入密码的情况下挂起

python - 根据文件类型为文件名添加 LasWriteTime 前缀

python - 使用 Selenium/Python 验证 div 中是否存在文本时出现问题

python - 卡尔曼滤波器 - 两个相同传感器的融合

Python CLI 进度条/旋转器,无需迭代

Python - 将多个相同维度的二维数组合并为一个二维数组

javascript - 如何扩展列表并选择动态值。

java - 如何使用 selenium 下载 POST 响应文件