The Yahoo website can open via port 2081 in a browser (set proxy port 2081 for HTTP and HTTPS in Firefox). Port 2081 provides an HTTP(S) proxy.
Port 2080 provides a SOCKS5 proxy service:
url="https://query1.finance.yahoo.com/v7/finance/download/MSFT"
curl --socks5-hostname 127.0.0.1:2080 $url -o msft.txt
I can download Yahoo data now and expect to use the yfinance library with this proxy.
Try method 1:
import yfinance as yf
msft = yf.Ticker("MSFT")
msft.history(proxy="http://127.0.0.1:2081")
msft.history(proxy="https://127.0.0.1:2081")
msft.history(proxy="socks5://127.0.0.1:2080")
None of them can work!They have same output:
MSFT: No price data found, symbol may be delisted (period=1mo)
Empty DataFrame
Columns: [Open, High, Low, Close, Adj Close, Volume]
Index: []
Try method 2:
cd ~
export all_proxy=socks5://127.0.0.1:2080
python3
Output:
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
Then
import yfinance as yf
msft = yf.Ticker("MSFT")
msft.history()
Output:
Failed to get ticker 'MSFT' reason: SOCKSHTTPSConnectionPool(host='query2.finance.yahoo.com', port=443): Read timed out. (read timeout=10)
MSFT: No price data found, symbol may be delisted (period=1mo)
Empty DataFrame
Columns: [Open, High, Low, Close, Adj Close, Volume]
Index: []
The same issue for export https_proxy=http://127.0.0.1:2081
.
Try method 3:
#pip install Pysocks first
import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 2080)
socket.socket = socks.socksocket
import yfinance as yf
msft = yf.Ticker("MSFT")
msft.history()
Error information:
Failed to get ticker 'MSFT' reason:
HTTPSConnectionPool(host='query2.finance.yahoo.com', port=443):
Max retries exceeded with url: /v8/finance/chart/MSFT?
range=1d&interval=1d&crumb=tCwRGfMyTIV (Caused by
NewConnectionError('<urllib3.connection.HTTPSConnection object at
0x7f78d3f89730>: Failed to establish a new connection: [Errno -2]
Name or service not known'))
How can I fix it then?
Updated already:
Request over the proxy:
yfinance over the proxy:
Yahoo never provides any service again.
The latest try for both the Bash command and Python:
port 2080 for socks5 proxy
is more stable than 2081(https proxy)
:
Fetch the data with https
proxy at last ,but can't get the data everytime.