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

1

6

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.

Oesophagus answered 1/1 at 1:1 Comment(4)
Can you remove the proxy in the yfinance method and show me the output?Lightsome
Also look at my answer, see if the proxy is reachable.Lightsome
Does this answer your question? Python requests.exceptions.SSLError: EOF occurred in violation of protocolDolora
Note: Please ensure to share all relevant debugging information when you are asking a question, you mentioned a "method 1" and said "None of them can work!" but never added the actual complete error traceback that you were getting. It was in your images that you later added where the actual error was shown, regarding which see: Why should I not upload images of code/data/errors?.Dolora
L
1

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")

Lightsome answered 4/1 at 6:15 Comment(12)
No proxy ,no yahoo service.With normal method ,the reponse can't be created,except occur when to call any yahoo's url ,name 'response' is not defined for response.status_code.Oesophagus
Where is your proxy sending connections to? I think you haven’t set it up properly. @Oesophagus can you try one of the free proxies here geonode.com/free-proxy-listLightsome
url="query1.finance.yahoo.com/v7/finance/download/MSFT"Oesophagus
curl --socks5-hostname 127.0.0.1:2080 $url -o msft.txtOesophagus
I can get the url over socks5 port 2080,it shows i set it properly,absolutely sure.Oesophagus
I added another method at the end of my answer can you give it a try and show me what happens ?Lightsome
Take a screenshot for both bash command and python code,the 2080 port is in good status!Oesophagus
Did you try the method right at the bottom?Lightsome
I have tried already,and update the post with image.Oesophagus
I added another suggestion, can you try it ?Lightsome
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.Oesophagus
You haven’t done what I said in the last suggestion. Did you set the os environment variables ?Lightsome

© 2022 - 2024 — McMap. All rights reserved.