
「Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。」の原因と解決方法について
Python初心者です。
【実際のコード】
web scrapingを行いたくseleniumを使って下記のコードを書きました。
python
1import time 2from selenium import webdriver 3import chromedriver_binary 4 5#Googleのブラウザを開く 6driver = webdriver.Chrome() 7driver.get('https://www.google.com/') 8time.sleep(5) 9driver.quit() 10 11#the guradianを検索 12search_box = driver.find_element_by_name('q') 13search_box.send_keys('the Guardian') 14search_box.submit() 15time.sleep(2)
しかしその結果、warningを除いて
「ConnectionRefusedError: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。」
というエラーが発生してしまい先に進めません。
【エラーの詳細】
Python
1Warning (from warnings module): 2 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\RnewHIST435-web-scraping.py", line 12 3 search_box = driver.find_element_by_name('q') 4DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead 5Traceback (most recent call last): 6 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connection.py", line 174, in _new_conn 7 conn = connection.create_connection( 8 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\util\connection.py", line 95, in create_connection 9 raise err 10 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\util\connection.py", line 85, in create_connection 11 sock.connect(sa) 12ConnectionRefusedError: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。 13 14During handling of the above exception, another exception occurred: 15 16Traceback (most recent call last): 17 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 703, in urlopen 18 httplib_response = self._make_request( 19 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 398, in _make_request 20 conn.request(method, url, **httplib_request_kw) 21 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connection.py", line 239, in request 22 super(HTTPConnection, self).request(method, url, body=body, headers=headers) 23 File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1282, in request 24 self._send_request(method, url, body, headers, encode_chunked) 25 File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1328, in _send_request 26 self.endheaders(body, encode_chunked=encode_chunked) 27 File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1277, in endheaders 28 self._send_output(message_body, encode_chunked=encode_chunked) 29 File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1037, in _send_output 30 self.send(msg) 31 File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 975, in send 32 self.connect() 33 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connection.py", line 205, in connect 34 conn = self._new_conn() 35 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connection.py", line 186, in _new_conn 36 raise NewConnectionError( 37urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001DB681E8E50>: Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。 38 39During handling of the above exception, another exception occurred: 40 41Traceback (most recent call last): 42 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\RnewHIST435-web-scraping.py", line 12, in <module> 43 search_box = driver.find_element_by_name('q') 44 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 665, in find_element_by_name 45 return self.find_element(by=By.NAME, value=name) 46 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 1248, in find_element 47 return self.execute(Command.FIND_ELEMENT, { 48 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 423, in execute 49 response = self.command_executor.execute(driver_command, params) 50 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\selenium\webdriver\remote\remote_connection.py", line 333, in execute 51 return self._request(command_info[0], url, body=data) 52 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\selenium\webdriver\remote\remote_connection.py", line 355, in _request 53 resp = self._conn.request(method, url, body=body, headers=headers) 54 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\request.py", line 78, in request 55 return self.request_encode_body( 56 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\request.py", line 170, in request_encode_body 57 return self.urlopen(method, url, **extra_kw) 58 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\poolmanager.py", line 376, in urlopen 59 response = conn.urlopen(method, u.request_uri, **kw) 60 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen 61 return self.urlopen( 62 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen 63 return self.urlopen( 64 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen 65 return self.urlopen( 66 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 785, in urlopen 67 retries = retries.increment( 68 File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\util\retry.py", line 592, in increment 69 raise MaxRetryError(_pool, url, error or ResponseError(cause)) 70urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=64766): Max retries exceeded with url: /session/6c5881dc3d4642675b89adf2b5179bec/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001DB681E8E50>: Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。'))
【試したこと】
- google chromeとchromedrive_binaryのバージョンを同じになるように設定しました。
- また、window10のプロキシ設定を自動的に検出するに変更しました。
しかし、特に変化がなく非常に困っています。
他の方法や具体的な解決策がある方、教えていただければ非常にうれしく思います。