Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 3.x Selenium-urllib.error.urleror:<;urlopen错误[Errno 61]连接被拒绝>;_Python 3.x_Selenium_Selenium Webdriver - Fatal编程技术网

Python 3.x Selenium-urllib.error.urleror:<;urlopen错误[Errno 61]连接被拒绝>;

Python 3.x Selenium-urllib.error.urleror:<;urlopen错误[Errno 61]连接被拒绝>;,python-3.x,selenium,selenium-webdriver,Python 3.x,Selenium,Selenium Webdriver,注意:我花了一个多小时试图解决这个问题,但没有找到适合我的解决方案 最后,结果证明这是一个非常简单的错误,但我想我会提出这个问题,以防其他人有同样的问题,可以快速找到解决方案 问题 我试图用以下代码刮取一个站点: phantomjs_path = '/Users/xxx/xxx/phantomjs-2.1.1-macosx/bin/phantomjs' driver = webdriver.PhantomJS(executable_path=phantomjs_path) driver.s

注意:我花了一个多小时试图解决这个问题,但没有找到适合我的解决方案

最后,结果证明这是一个非常简单的错误,但我想我会提出这个问题,以防其他人有同样的问题,可以快速找到解决方案


问题 我试图用以下代码刮取一个站点:

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
并得到以下错误:

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>
回溯(最近一次呼叫最后一次):
dou open中的文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”,第1254行
h、 请求(请求获取方法(),请求选择器,请求数据,标题)
请求中的文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”,第1106行
self.\u发送请求(方法、url、正文、标题)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”,第1151行,在发送请求中
self.endheaders(主体)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”,第1102行,在endheaders中
自发送输出(消息体)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”,第934行,在发送输出中
self.send(msg)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”,第877行,在send中
self.connect()
connect中的文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py”,第849行
(self.host、self.port)、self.timeout、self.source\u地址)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py”,第711行,位于create_connection中
提出错误
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py”,第702行,位于create_connection中
sock.connect(sa)
ConnectionRefusedError:[Errno 61]连接被拒绝
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“my_script.py”,第1251行,在
MyObject.script_main()
脚本主目录中的文件“my_script.py”,第1232行
self.parse_js(url)
parse_js中的文件“my_script.py”,第1202行
打印('response:',response.text)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/selenium/webdriver/remote/webelement.py”,第68行,文本
返回self.\u执行(Command.GET\u元素\u文本)['value']
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/selenium/webdriver/remote/webelement.py”,第461行,在
返回self.\u parent.execute(命令,参数)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/selenium/webdriver/remote/webdriver.py”,执行中的第234行
响应=self.command\u executor.execute(driver\u command,params)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/selenium/webdriver/remote/remote\u connection.py”,执行中第401行
返回self.\u请求(命令信息[0],url,正文=数据)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site packages/selenium/webdriver/remote/remote\u connection.py”,第471行,in\u请求
resp=opener.open(请求,超时=self.\u超时)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”,第466行,打开
响应=自身打开(请求,数据)
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”,第484行,打开
"开放",
文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”,第444行,在调用链中
结果=func(*args)
http_open中的文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”,第1282行
返回self.do_open(http.client.HTTPConnection,req)
dou open中的文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/urllib/request.py”,第1256行
引发URL错误(err)
urllib.error.urleror:
在Chrome浏览器中手动加载url正在工作

无论如何,我试着将url从
https
切换到
http
,但还是出现了相同的错误

此外,在前一天,我没有收到任何错误,因此我认为这不可能是防火墙的问题,正如我在其他一些问题中看到的那样


查看解决方案的答案…

结果表明,显然我已将行
driver.quit()
向上移动,因此在调用“get\u atribute”时引发了错误

解决方案 只需向下移动
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()