【问题标题】:Errno 61 Connection refused when using Selenium XPath on Yahoo Finance dynamic content在 Yahoo Finance 动态内容上使用 Selenium XPath 时 Errno 61 连接被拒绝
【发布时间】:2017-10-22 22:31:53
【问题描述】:

我运行以下代码来获取分析师评级(股票页面右侧的动态内容-附图片)Analyst Recommendation picture

# Import libraries
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

link_base = 'https://ca.finance.yahoo.com/quote/'
stock_link = link_base + 'RYAAY' + '?p=' + 'RYAAY'

driver = webdriver.Chrome()
driver.maximize_window()
driver.get(stock_link)
timeout = 10
try:
    WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="Col2-6-QuoteModule-Proxy"]/div/section/a/h2/span')))
except TimeoutException:
    print("Timed out waiting for page to load")
    driver.quit()

analyst_rating = driver.find_element_by_xpath('//*[@id="Col2-6-QuoteModule-Proxy"]/div/section/div/div/div')
print (analyst_rating.text)
driver.quit()

作为回报,我得到 Connection Refused Errno 61。最奇怪的是,这一直给我一个错误。然后我重新启动 PyCharm,它产生了所需的数字“2”,但只有一次。它再也没有工作过,已经好几个小时了。这里可能发生什么?任何帮助将不胜感激。

Timed out waiting for page to load
Traceback (most recent call last):
  File "/Users/nikolaysitnikov/Desktop/google-python-exercises/Stocks scrape/yahoo_unit.py", line 29, in <module>
    analyst_rating = driver.find_element_by_xpath('//*[@id="Col2-6-QuoteModule-Proxy"]/div/section/div/div/div')
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 843, in find_element
    'value': value})['value']
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 306, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 464, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 487, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 936, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

【问题讨论】:

    标签: python-3.x selenium-webdriver web-scraping selenium-chromedriver


    【解决方案1】:

    由于您的计算机和您尝试访问的网络服务器之间存在网络问题,因此引发了错误。如果我不得不猜测可能是什么问题,我会说您的防火墙正在阻止 selenium 的请求通过。在您的机器设置中查找防火墙访问提示。如果没有,请尝试重新安装 chromeriver。

    如果您尝试进行任何可靠的网络抓取,我建议您切换到requestsscrapy。 Selenium 对很多事情都有好处,但并不可悲。

    【讨论】:

    • 感谢您的评论。我想到了。我实际上连接良好。原来这些元素直到向下滚动才出现。所以我不得不使用 Selenium 向下滚动页面,一切正常。
    猜你喜欢
    • 2011-08-02
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2019-08-07
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多