简体   繁体   English

Python JavaScript爬网:ConnectionRefusedError:[Errno 61]连接被拒绝

[英]Python JavaScript Scraping: ConnectionRefusedError: [Errno 61] Connection refused

I am trying to scrape some info off of Yahoo Finance. 我正在尝试从Yahoo Finance中抓取一些信息。 Below is my simple code: 下面是我的简单代码:

url = "https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL"
from selenium import webdriver
webdriver.PhantomJS(executable_path="/anaconda/bin/phantomjs")
browser.get(url)
browser.quit

But I get the following error: 但是我收到以下错误:

ConnectionRefusedError                    Traceback (most recent call last)
<ipython-input-12-f4d26b367ef7> in <module>()
  8 webdriver.PhantomJS(executable_path="/anaconda/bin/phantomjs")
  9 
---> 10 browser.get(url)

> /anaconda/lib/python3.6/site-packages/selenium-3.4.3-        py3.6.egg/selenium/webdriver/remote/webdriver.py in get(self, url)
266         Loads a web page in the current browser session.
267         """
--> 268         self.execute(Command.GET, {'url': url})
269 
270     @property

/anaconda/lib/python3.6/site-packages/selenium-3.4.3-py3.6.egg/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
252 
253         params = self._wrap_value(params)
--> 254         response = self.command_executor.execute(driver_command, params)
255         if response:
256             self.error_handler.check_response(response)

/anaconda/lib/python3.6/site-packages/selenium-3.4.3-py3.6.egg/selenium/webdriver/remote/remote_connection.py in execute(self, command, params)
462         path = string.Template(command_info[1]).substitute(params)
463         url = '%s%s' % (self._url, path)
--> 464         return self._request(command_info[0], url, body=data)
465 
466     def _request(self, method, url, body=None):

/anaconda/lib/python3.6/site-packages/selenium-3.4.3-py3.6.egg/selenium/webdriver/remote/remote_connection.py in _request(self, method, url, body)
485                 body = None
486             try:
--> 487                 self._conn.request(method, parsed_url.path, body, headers)
488                 resp = self._conn.getresponse()
489             except (httplib.HTTPException, socket.error):

/anaconda/lib/python3.6/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1237                 encode_chunked=False):
   1238         """Send a complete request to the server."""
-> 1239         self._send_request(method, url, body, headers, encode_chunked)
   1240 
   1241     def _send_request(self, method, url, body, headers, encode_chunked):

/anaconda/lib/python3.6/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1283             # default charset of iso-8859-1.
   1284             body = _encode(body, 'body')
-> 1285         self.endheaders(body, encode_chunked=encode_chunked)
   1286 
   1287     def getresponse(self):

/anaconda/lib/python3.6/http/client.py in endheaders(self, message_body, encode_chunked)
   1232         else:
   1233             raise CannotSendHeader()
-> 1234         self._send_output(message_body, encode_chunked=encode_chunked)
   1235 
   1236     def request(self, method, url, body=None, headers={}, *,

/anaconda/lib/python3.6/http/client.py in _send_output(self, message_body, encode_chunked)
   1024         msg = b"\r\n".join(self._buffer)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 
   1028         if message_body is not None:

/anaconda/lib/python3.6/http/client.py in send(self, data)
962         if self.sock is None:
963             if self.auto_open:
--> 964                 self.connect()
965             else:
966                 raise NotConnected()

/anaconda/lib/python3.6/http/client.py in connect(self)
934         """Connect to the host and port specified in __init__."""
935         self.sock = self._create_connection(
--> 936             (self.host,self.port), self.timeout, self.source_address)
937         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
938 

/anaconda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
720 
721     if err is not None:
--> 722         raise err
723     else:
724         raise error("getaddrinfo returns an empty list")

/anaconda/lib/python3.6/socket.py in create_connection(address, timeout, source_address)
711             if source_address:
712                 sock.bind(source_address)
 713             sock.connect(sa)
714             return sock
715 

 ConnectionRefusedError: [Errno 61] Connection refused

I have checked other questions on this topic but couldn't find any relevant answers. 我已经检查了有关此主题的其他问题,但找不到任何相关答案。 How can I correct this error? 我该如何纠正该错误?

Here is the Answer to your Question: 这是您的问题的答案:

The error stack trace gives us a hint that the actual issue may be in this line of the code: 错误堆栈跟踪为我们提供了一个提示,即实际问题可能在代码的以下行中:

webdriver.PhantomJS(executable_path="/anaconda/bin/phantomjs")

As you are trying to open the URL through the instance browser as in browser.get(url) , the browser instance must be configured with the absolute path of the PhantomJS executable as follows (as on my Win8 machine through PyCharm IDE): 当您尝试像在browser.get(url)那样通过实例browser打开URL时, browser实例必须使用PhantomJS可执行文件的absolute path进行如下配置(例如在我的Win8机器上通过PyCharm IDE):

from selenium import webdriver

url = "https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL"
browser = webdriver.PhantomJS(executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
browser.get(url)
# your code block
print(browser.title)
# your code block
browser.quit

The output on my console is as follows: 我的控制台上的输出如下:

AAPL Key Statistics | Apple Inc. Stock - Yahoo Finance

Let me know if this Answers your Question. 让我知道这是否回答了您的问题。

暂无
暂无

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

相关问题 Python ConnectionRefusedError: [Errno 61] 连接被拒绝 - Python ConnectionRefusedError: [Errno 61] Connection refused ConnectionRefusedError:[Errno 61] Mac 上的连接被拒绝 Python3.8.5 - ConnectionRefusedError: [Errno 61] Connection refused Python3.8.5 on Mac ConnectionRefusedError:[Errno 111]连接被拒绝 - ConnectionRefusedError: [Errno 111] Connection refused ConnectionRefusedError:[Errno 61]连接被拒绝&无法连接到端点URL:http://127.0.0.1:3001 - ConnectionRefusedError: [Errno 61] Connection refused & Could not connect to the endpoint URL: http://127.0.0.1:3001 Ftplib ConnectionRefusedError: [Errno 111] 连接被拒绝 (python 3.5) - Ftplib ConnectionRefusedError: [Errno 111] Connection refused (python 3.5) python 套接字返回 ConnectionRefusedError: [Errno 111] 连接被拒绝 - python socket return ConnectionRefusedError: [Errno 111] Connection refused python-valve rcon minecraft&#39;ConnectionRefusedError:[Errno 111]连接被拒绝&#39; - python-valve rcon minecraft 'ConnectionRefusedError: [Errno 111] Connection refused' Python ConnectionRefusedError:[Errno 111] Docker 容器上的连接被拒绝 - Python ConnectionRefusedError: [Errno 111] Connection refused on Docker container 使用 python ftplib 下载但得到 ConnectionRefusedError: [Errno 111] Connection refused - use python ftplib downloading but get ConnectionRefusedError: [Errno 111] Connection refused Sendmail 错误[61] 连接被拒绝 - Sendmail Errno[61] Connection Refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM