How can I make yfinance work over HTTP(S) or a socks5 proxy?

666 Views Asked by At

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:

Enter image description here

Request over the proxy:

Enter image description here

yfinance over the proxy:

Enter image description here

Yahoo never provides any service again.

Enter image description here

The latest try for both the Bash command and Python:

Enter image description here

port 2080 for socks5 proxy is more stable than 2081(https proxy):

enter image description here

Fetch the data with https proxy at last ,but can't get the data everytime.

1

There are 1 best solutions below

12
anarchy On

I don’t have a proxy server, but can you try this and see if you can access the website?

import requests

proxies = {
    'http': 'http://127.0.0.1:2081',
    'https': 'https://127.0.0.1:2081',
}

response = requests.get("https://query1.finance.yahoo.com/v7/finance/download/MSFT", proxies=proxies)

print(response.text)

Also try the following. Make sure you have the latest version of yfinance, using pip install yfinance --upgrade --no-cache-dir in the command line, and then run this.

import yfinance as yf

msft = yf.Ticker("MSFT")

msft.history(period="1mo", proxy="https://127.0.0.1:2081")

Alternatively, you can also try:

df = yf.download("MSFT", period="max", proxy="https://127.0.0.1:2081")

If there are errors, please show me.

Try the following as well, to see if your proxy is reachable. Use your proxy address and ports.

import requests

proxies = {
   "http": "http://USERNAME:[email protected]:7777",
   "https": "http://USERNAME:[email protected]:7777"
}

response=requests.get("http://httpbin.org/ip", proxies=proxies)

print("Response Status Code", response.status_code)

print("Response data in Content format:\n", response.content)

Also try using the normal yfinance method without a proxy and show me the output as well.

Try this method and let me know what happens.

import requests
import yfinance as yf

proxies = {
    'http': 'socks5://127.0.0.1:2080',
    'https': 'socks5://127.0.0.1:2080'
}

session = requests.Session()
session.proxies = proxies
yf.pdr_override(session)

data = yf.download('MSFT', start='2023-01-01', end='2024-01-01')

print(data.head())

Update: It seems that yfinance doesn’t have proper support for socks5 proxy.

I suggest you try redirecting all traffic in your computer through the socks5, then run your code as per normal like this.


import os

proxy = 'http://<user>:<pass>@<proxy>:<port>'

os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

import yfinance as yf

msft = yf.Ticker("MSFT")

msft.history(period="1mo")