gpt4 book ai didi

selenium - 如何解决 urllib3.exceptions.MaxRetryError : HTTPConnectionPool(host ='127.0.0.1' , port=58408): Max retries exceeded with url

转载 作者:行者123 更新时间:2023-12-03 23:42:49 28 4
gpt4 key购买 nike

我正在尝试用 selenium 抓取网站的几页并使用结果,但是当我运行该函数两次时

[WinError 10061] No connection could be made because the target machine actively refused it'
第二个函数调用出现错误。
这是我的方法:
import os
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup as soup

opts = webdriver.ChromeOptions()
opts.binary_location = os.environ.get('GOOGLE_CHROME_BIN', None)
opts.add_argument("--headless")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--no-sandbox")
browser = webdriver.Chrome(executable_path="CHROME_DRIVER PATH", options=opts)

lst =[]
def search(st):
for i in range(1,3):
url = "https://gogoanime.so/anime-list.html?page=" + str(i)
browser.get(url)
req = browser.page_source
sou = soup(req, "html.parser")
title = sou.find('ul', class_ = "listing")
title = title.find_all("li")
for j in range(len(title)):
lst.append(title[j].getText().lower()[1:])
browser.quit()
print(len(lst))

search("a")
search("a")
输出
272
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=58408): Max retries exceeded with url: /session/4b3cb270d1b5b867257dcb1cee49b368/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001D5B378FA60>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

最佳答案

这个错误信息...

raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=58408): Max retries exceeded with url: /session/4b3cb270d1b5b867257dcb1cee49b368/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001D5B378FA60>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
...暗示未能建立新的连接引发 MaxRetryError 因为无法建立连接。

几件事:
  • 首先,根据讨论max-retries-exceeded exceptions are confusing追溯有些误导。为方便用户,请求包装了异常。原始异常是显示消息的一部分。
  • 请求从不重试(它为 urllib3 的 retries=0 设置了 HTTPConnectionPool),所以如果没有 ,错误会更加规范。 MaxRetryError HTTPConnectionPool 关键词。所以理想的回溯应该是:
      ConnectionError(<class 'socket.error'>: [Errno 1111] Connection refused)

  • 根本原因和解决方案
    一旦您启动了 webdriver 和 web 客户端 session ,接下来是 def search(st)您正在调用 get() o 访问一个 url 并在随后的行中调用 browser.quit()用于调用 /shutdown端点,随后 webdriver 和 web 客户端实例被完全销毁,关闭所有页面/选项卡/窗口。因此不再存在连接。

    You can find a couple of relevant detailed discussion in:


    在下一次迭代中的这种情况下(由于 for 循环)当 browser.get()被调用时没有事件连接。因此你会看到错误。
    所以一个简单的解决方案是删除行 browser.quit()并调用 browser.get(url)在相同的浏览上下文中。

    结论
    一旦升级到 Selenium 3.14.1 您将能够设置超时并查看规范的回溯,并能够采取必要的行动。

    引用
    您可以在以下位置找到相关的详细讨论:
  • MaxRetryError: HTTPConnectionPool: Max retries exceeded (Caused by ProtocolError('Connection aborted.', error(111, 'Connection refused')))

  • tl;博士
    几个相关的讨论:
  • Adding max_retries as an argument
  • Removed the bundled charade and urllib3.
  • Third party libraries committed verbatim
  • 关于selenium - 如何解决 urllib3.exceptions.MaxRetryError : HTTPConnectionPool(host ='127.0.0.1' , port=58408): Max retries exceeded with url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64745726/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com