简体   繁体   English

Selenium - urllib.error.URLError: <urlopen error [Errno 61] Connection refused>

[英]Selenium - urllib.error.URLError: <urlopen error [Errno 61] Connection refused>

Note: I spend more than one hour trying to solve this issue and found no solution that worked for me. 注意:我花了一个多小时试图解决这个问题,发现没有适用于我的解决方案。

At the end it turned out to be a very simple mistake, but I thought I will create the question so in case anybody else has the same issue can find a solution fast. 最后它被证明是一个非常简单的错误,但我想我会创建这个问题,以防其他人有同样的问题可以快速找到解决方案。


Problem 问题

I was trying to scrape a site with the following code: 我试图用以下代码刮取网站:

phantomjs_path = '/Users/xxx/xxx/phantomjs-2.1.1-macosx/bin/phantomjs'

driver = webdriver.PhantomJS(executable_path=phantomjs_path)

driver.set_window_size(1024, 768) #optional

driver.get(url)

# wait
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "flightrow")))

response = driver.find_element_by_css_selector('table[class="flighttable"]')

driver.quit()

html = response.get_attribute('outerHTML') #pass from webdrive object to string

And was getting the following error: 并得到以下错误:

Traceback (most recent call last):


File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 849, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 711, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 702, 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 "my_script.py", line 1251, in <module>
    MyObject.script_main()
  File "my_script.py", line 1232, in script_main
    self.parse_js(url)
  File "my_script.py", line 1202, in parse_js
    print('response:', response.text)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 68, in text
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 461, in _execute
    return self._parent.execute(command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 401, in execute
    return self._request(command_info[0], url, body=data)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 471, in _request
    resp = opener.open(request, timeout=self._timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1282, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py", line 1256, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 61] Connection refused>

Loading the url manually in the Chrome browser was working. 在Chrome浏览器中手动加载网址正常。

Anyway, I tried switching the url from https to http , but I still got the same error. 无论如何,我尝试将网址从https切换到http ,但我仍然遇到了同样的错误。

In addition, during the previous day I did not get any error, so I assumed it could not be a problem with firewalls, as I read in some other questions. 另外,在前一天我没有收到任何错误,所以我认为它不是防火墙的问题,因为我在其他一些问题中读到了。

See answer for the solution... 查看解决方案的答案......

It turned out that apparently I had moved the line driver.quit() upwards, so the error was raised when calling 'get_atribute'. 事实证明,显然我已将行driver.quit()向上移动,因此在调用'get_atribute'时出现了错误。

Solution

Just move driver.quit() downwards: 只需向下移动driver.quit()

driver = webdriver.PhantomJS(executable_path=phantomjs_path)

driver.set_window_size(1024, 768) #optional

driver.get(url)

# wait
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "flightrow")))

response = driver.find_element_by_css_selector('table[class="flighttable"]')

html = response.get_attribute('outerHTML') #pass from webdrive object to string

#do not move quite() upwards! even if 'driver' is not specifically called with the command 'get_attribute'
#it will raise an error if driver is closed.
driver.quit()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 urllib.error.URLError:<urlopen error [errno 113] no route to host></urlopen> - urllib.error.URLError: <urlopen error [Errno 113] No route to host> Python3 urllib.error.URLError:<urlopen error [errno 110] connection timed out> 在服务器上</urlopen> - Python3 urllib.error.URLError: <urlopen error [Errno 110] Connection timed out> on server Python3 Wolframalpha 给出错误:urllib.error.URLError<urlopen error [Errno 101] Network is unreachable> - Python3 Wolframalpha Giving Error: urllib.error.URLError <urlopen error [Errno 101] Network is unreachable> Beautiful Soup urllib.error.URLError: urlopen error [Errno -2] Name or service not known [Python scraper] - Beautiful Soup urllib.error.URLError: urlopen error [Errno -2] Name or service not known [Python scraper] python - urllib.error.URLError:<urlopen error timed out> - python - urllib.error.URLError: <urlopen error timed out> urllib.error.URLError: <urlopen error no host given> python 3 - urllib.error.URLError: <urlopen error no host given> python 3 urllib.error.URLError:<urlopen error unknown url type: 'https> - urllib.error.URLError: <urlopen error unknown url type: 'https> urllib.error.URLError:HTTP错误403:urllib.request.urlopen禁止 - urllib.error.URLError: HTTP Error 403: Forbidden from urllib.request.urlopen python3.6.5 urllib.error.URLError:<urlopen error unknown url type: https> - python3.6.5 urllib.error.URLError: <urlopen error unknown url type: https> 如何将HTML表格读取为资料框(urllib.error.URLError: <urlopen error unknown url type: https> )? - How to read html table as dataframe (urllib.error.URLError: <urlopen error unknown url type: https>)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM