ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
14.61k stars 2.43k forks source link

Why can't get data via proxy? #1811

Open hwypengsir opened 10 months ago

hwypengsir commented 10 months ago

The port 2080 can be used to proxy request for yahoo finance server.

url="https://query1.finance.yahoo.com/v7/finance/download/MSFT?period1=1672397200&period2=1703933200&interval=1d&events=history&includeAdjustedClose=true"
curl --socks5-hostname 127.0.0.1:2080 $url  -o msft.txt

Now i want to get the data with yfinance library.

import socket
import socks
socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 2080)
socket.socket = socks.socksocket
import yfinance as yf
data = yf.download('MSFT', '2000-01-01', '2020-01-01')

It encounter issue:

Failed to get ticker 'MSFT' reason: HTTPSConnectionPool(host='query2.finance.yahoo.com', port=443): Read timed out. (read timeout=10)
[*********************100%%**********************]  1 of 1 completed

1 Failed download:
['MSFT']: Exception('%ticker%: No timezone found, symbol may be delisted')

How can fix it?

ValueRaider commented 10 months ago

Proxy functionality isn't covered by unit tests so could break without knowing, and most (all?) developers don't use proxy so you have to debug and fix. If you know of a good free proxy, then please consider adding unit tests.

First, try dev branch #1080

m666m commented 6 months ago

At present, at least 1.4 billion / 8 billion people realy need to use this function.

The simplest way to test using a proxy is SSH port forward:

$ ssh -CND 12345 -p 22 user@another_pc

Keep it running.

Then create a new tty, get something through the proxy:

$ curl -vvv -x socks5://localhost:12345 https://www.yahoo.com

Or, provide a way to shutdown lru_cache, let the user self-manage the access rate limit.

THK a lot!

m666m commented 6 months ago

@hwypengsir , try code below:

import yfinance as yf

saddr = 'socks5h://localhost:12345' 

yf.Ticker('MSFT').history('1mo', proxy={"http": saddr, "https": saddr})

I fixed my problem by set proxy in yf.Ticker(...).

It's NOT a lru_cache problem.