10061 対象のコンピューターによって拒否されたため、接続できませんでした。

�yPython�z"ConnectionRefusedError: [WinError 10061] �Ώۂ̃R���s���[�^�[�ɂ���ċ��ۂ��ꂽ���߁A�ڑ��ł��܂���ł���"�̌����ƑΏ� | �C���^�[�l�b�g����t�@�C�����_�E�����[�h����T���v��

urllib.request.urlretrieve �ŃC���^�[�l�b�g��̃t�@�C�����_�E�����[�h���悤�Ƃ����Ƃ��뎟�̃G���[���������܂����B


ConnectionRefusedError: [WinError 10061] �Ώۂ̃R���s���[�^�[�ɂ���ċ��ۂ��ꂽ���߁A�ڑ��ł��܂���ł���

�F�X�Ȍ������l������Ǝv���܂��B
����� https �ɑΉ����Ă��Ȃ� Web�T�[�o�ɑ΂��� http �Ŏ擾���悤�Ƃ��Ă��̃G���[���������Ă��܂����B
http �ɕύX���Ė����_�E�����[�h���”\�ƂȂ�܂����B

�y�ԈႢ�z

import urllib.request

url='https://tooljp.com/test.txt'

# \\escape
download_file_name='c:\\temp\\test2.txt'

urllib.request.urlretrieve(url, download_file_name)

Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 1342, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1255, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1301, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1250, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1010, in _send_output
self.send(msg)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 950, in send
self.connect()
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1417, in connect
super().connect()
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 921, in connect
self.sock = self._create_connection(
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\socket.py", line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] �Ώۂ̃R���s���[�^�[�ɂ���ċ��ۂ��ꂽ���߁A�ڑ��ł��܂���ł����B

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\temp\test.py", line 8, in

urllib.request.urlretrieve(url, download_file_name)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 239, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\urllib\request.py", line 214, in urlopen
�ȉ��ȗ�

�T���v���R�[�h

�ȉ��� Windows 10 �‹��ɂ����鐳����s�”\�ȃT���v���R�[�h�ł��B

Windows �‹��ɂ�����C���^�[�l�b�g���Web�T�[�o����t�@�C�����_�E�����[�h����T���v�����Љ�܂��B
urllib ��URL�𗘗p���邽�߂֗̕��ȃ��W���[���Q�ł��B
���̒��� urllib.request �� URLs ���擾���邽�߂� Python �̃��W���[���ł��B

���̃T���v���ł�Web�T�[�o�ォ��t�@�C�����_�E�����[�h���܂��B

download_file_name �ɂ̓t�@�C�����_�E�����[�h���郍�[�J���̃t�@�C�������w�肵�܂��B

�T���v���R�[�h�F

import urllib.request

url='http://tooljp.com/test.txt'

# \\escape
download_file_name='c:\\temp\\test2.txt'

urllib.request.urlretrieve(url, download_file_name)


�{�T�C�g���f�ڂ���Ă�����́A���쌠�@�ɂ��ی삳��Ă��܂��B�����Ȃ�ꍇ�ł������҂̋��‚Ȃ��R�s�[�A�z�z���邱�Ƃ͂ł��܂���B ���̃y�[�W�̓����N�t���[�ł��B(���̃y�[�W�ւ̒��ڃ����N���”\�ł��B)

「Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。」の原因と解決方法について

Python初心者です。

【実際のコード】
web scrapingを行いたくseleniumを使って下記のコードを書きました。

python

import time from selenium import webdriver import chromedriver_binary #Googleのブラウザを開く driver = webdriver.Chrome() driver.get('https://www.google.com/') time.sleep(5) driver.quit() #the guradianを検索 search_box = driver.find_element_by_name('q') search_box.send_keys('the Guardian') search_box.submit() time.sleep(2)

しかしその結果、warningを除いて
「ConnectionRefusedError: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。」
というエラーが発生してしまい先に進めません。

【エラーの詳細】

Python

Warning (from warnings module): File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\RnewHIST435-web-scraping.py", line 12 search_box = driver.find_element_by_name('q') DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead Traceback (most recent call last): 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 conn = connection.create_connection( 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 raise err 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 sock.connect(sa) ConnectionRefusedError: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 703, in urlopen httplib_response = self._make_request( 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 conn.request(method, url, **httplib_request_kw) File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connection.py", line 239, in request super(HTTPConnection, self).request(method, url, body=body, headers=headers) File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1282, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1328, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1277, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1037, in _send_output self.send(msg) File "C:\Users\ユーザー名\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 975, in send self.connect() File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connection.py", line 205, in connect conn = self._new_conn() 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 raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001DB681E8E50>: Failed to establish a new connection: [WinError 10061] 対象のコンピューターによって拒否されたため、接続できませんでした。 During handling of the above exception, another exception occurred: Traceback (most recent call last): 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> search_box = driver.find_element_by_name('q') 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 return self.find_element(by=By.NAME, value=name) 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 return self.execute(Command.FIND_ELEMENT, { 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 response = self.command_executor.execute(driver_command, params) 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 return self._request(command_info[0], url, body=data) 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 resp = self._conn.request(method, url, body=body, headers=headers) File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\request.py", line 78, in request return self.request_encode_body( 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 return self.urlopen(method, url, **extra_kw) File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\poolmanager.py", line 376, in urlopen response = conn.urlopen(method, u.request_uri, **kw) File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen return self.urlopen( File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen return self.urlopen( File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 813, in urlopen return self.urlopen( File "C:\Users\ユーザー名\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\urllib3\connectionpool.py", line 785, in urlopen retries = retries.increment( 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 raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.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のプロキシ設定を自動的に検出するに変更しました。

しかし、特に変化がなく非常に困っています。

他の方法や具体的な解決策がある方、教えていただければ非常にうれしく思います。