Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium-无法建立新连接:[Errno 61]连接被拒绝_Python_Python 3.x_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

Python selenium-无法建立新连接:[Errno 61]连接被拒绝

Python selenium-无法建立新连接:[Errno 61]连接被拒绝,python,python-3.x,selenium,selenium-webdriver,selenium-chromedriver,Python,Python 3.x,Selenium,Selenium Webdriver,Selenium Chromedriver,我正在尝试为我正在做的一个项目获取一个网站的截图。然而,当我运行我的代码时,它第一次工作并给我截图。但是,当代码循环拍摄另一个屏幕截图时,会出现一条很长的错误消息,表示连接已被拒绝。我正在MacBookAir上使用python 3和selenium 这是我的密码 from datetime import datetime from selenium import webdriver from selenium.webdriver.chrome.options import Options op

我正在尝试为我正在做的一个项目获取一个网站的截图。然而,当我运行我的代码时,它第一次工作并给我截图。但是,当代码循环拍摄另一个屏幕截图时,会出现一条很长的错误消息,表示连接已被拒绝。我正在MacBookAir上使用python 3和selenium

这是我的密码

from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions() 

options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument(f'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36')

driver = webdriver.Chrome('/Library/Frameworks/Python.framework/Versions/3.8/bin/chromedriver', options=options)

URL1 = 'https://www.accuweather.com/en/ie/moroe/1079356/current-weather/1079356'
URL2 = 'https://www.accuweather.com/en/ie/moroe/1079356/hourly-weather-forecast/1079356'
URL3 = 'https://weather.com/en-IE/weather/today/l/d71e95387799a552a061ec1550ac876dcc19b5d139adc6f51ba3b8bf7a6b96ed'
URL4 = 'https://weather.com/en-IE/weather/hourbyhour/l/d71e95387799a552a061ec1550ac876dcc19b5d139adc6f51ba3b8bf7a6b96ed#detailIndex4'
URL5 = 'https://www.met.ie/weather-forecast/moroe-limerick#forecasts'

while True:
    current_time = datetime.now()
    timenow = datetime.now()
    timenow = str(timenow)
    current_time = str(current_time)
    
    new_str = ""
    x = 0

    for i in range(0, len(current_time)):
        if i != 4 and i != 7 and i != 10 and i != 13 and i != 16:
            new_str = new_str + current_time[i]

    new_str = float(new_str)
    new_str = new_str / 100
    new_str = round(new_str, 0)
    if new_str % 2:
        x = x + 1
    else:        
        driver.get(URL1)

        S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
        driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
        driver.find_element_by_tag_name('body').screenshot('accu1' + timenow + '.png')
    
        driver.quit()

        time.sleep(61)
'''
        driver.get(URL2)

        S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
        driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
        driver.find_element_by_tag_name('body').screenshot('accu2' + timenow + '.png')
    
        driver.quit()

        driver.get(URL3)

        S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
        driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
        driver.find_element_by_tag_name('body').screenshot('weatherchannel1' + timenow + '.png')
    
        driver.quit()

        driver.get(URL4)

        S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
        driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
        driver.find_element_by_tag_name('body').screenshot('weatherchannel2' + timenow + '.png')
    
        driver.quit()

        driver.get(URL5)

        S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
        driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
        driver.find_element_by_tag_name('body').screenshot('meteireann' + timenow + '.png')
    
        driver.quit()
'''
下面是错误消息

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/connection.py", line 96, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/connection.py", line 86, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 394, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 234, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1240, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1286, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1235, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1006, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 946, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 200, in connect
    conn = self._new_conn()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py", line 181, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fb4e73fd490>: Failed to establish a new connection: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/hugophelan/Desktop/WeatherPiTest.py", line 39, in <module>
    driver.get(URL1)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 374, in execute
    return self._request(command_info[0], url, body=data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 397, in _request
    resp = self._conn.request(method, url, body=body, headers=headers)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 78, in request
    return self.request_encode_body(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/request.py", line 170, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/poolmanager.py", line 375, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 783, in urlopen
    return self.urlopen(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 783, in urlopen
    return self.urlopen(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 783, in urlopen
    return self.urlopen(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=54000): Max retries exceeded with url: /session/3bdcabee5f314f620196394cfedd7079/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb4e73fd490>: Failed to establish a new connection: [Errno 61] Connection refused'))
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connection.py”,第169行,位于康涅狄格州新州
conn=connection.create\u连接(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/util/connection.py”,第96行,在create_connection中
提出错误
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/util/connection.py”,第86行,在create_connection中
sock.connect(sa)
ConnectionRefusedError:[Errno 61]连接被拒绝
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connectionpool.py”,urlopen中第699行
httplib\u response=self.\u发出请求(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connectionpool.py”,第394行,在请求中
conn.request(方法,url,**httplib\u request\u kw)
请求中的文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connection.py”,第234行
super(HTTPConnection,self).request(方法,url,body=body,headers=headers)
请求中的文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py”,第1240行
self.\u发送\u请求(方法、url、正文、标题、编码\u分块)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py”,第1286行,在发送请求中
self.endheaders(body,encode\u chunked=encode\u chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py”,第1235行,在endheaders中
self.\u发送\u输出(消息体,encode\u chunked=encode\u chunked)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py”,第1006行,在发送输出中
self.send(msg)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py”,第946行,在send中
self.connect()
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connection.py”,第200行,在connect中
conn=自我。_new_conn()
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connection.py”,康涅狄格州新州第181行
提出新的连接错误(
urllib3.exceptions.NewConnectionError::无法建立新连接:[Errno 61]连接被拒绝
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Users/hugophelan/Desktop/WeatherPiTest.py”,第39行,在
获取驱动程序(URL1)
get中的文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/webdriver.py”,第333行
self.execute(Command.GET,{'url':url})
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/webdriver.py”,执行中第319行
响应=self.command\u executor.execute(driver\u command,params)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/remote_connection.py”,第374行,在execute中
返回self.\u请求(命令信息[0],url,正文=数据)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/selenium/webdriver/remote/remote\u connection.py”,第397行,in\u请求
resp=self.\u conn.request(方法、url、body=body、headers=headers)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/request.py”,请求中第78行
返回self.request\u encode\u body(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/request.py”,第170行,在request\u encode\u正文中
返回self.urlopen(方法,url,**额外\u kw)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/poolmanager.py”,urlopen中第375行
response=conn.urlopen(方法,u.request\u uri,**kw)
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connectionpool.py”,第783行,在urlopen中
返回self.urlopen(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connectionpool.py”,第783行,在urlopen中
返回self.urlopen(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connectionpool.py”,第783行,在urlopen中
返回self.urlopen(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/connectionpool.py”,第755行,在urlopen中
重试次数=重试次数。增量(
文件“/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site packages/urllib3/util/retry.py”,第573行,增量
引发MaxRetryError(_池、url、错误或响应错误(原因))
urllib3.exceptions.MaxRetryError:HTTPConnectionPool(host='127.0.0.1',port=54000):url:/session/3bdcabee5f314f620196394cfedd7079/url超过了最大重试次数(由NewConnectionError引起(':未能建立新连接:[Errno 61]连接被拒绝'))

那是因为你退出了连接,看看它做了什么。删除行
driver.quit()
。你应该会没事。(当你真的想结束程序时,只保留最后一行)那是因为你退出了连接,看看它做了什么。删除行
driv