0

I have been using the following code for a while to extract stock price from yahoo finance. This code is now generating an error saying it cannot read the url.

import pandas_datareader.data as web
stock = web.DataReader(i_allStock+'.L', 'yahoo', start, end)

Has anyone had this problem and found a solution?

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
  • 1
    related: https://stackoverflow.com/questions/68226884/how-to-fix-new-unable-to-read-url-error-in-python-for-yahoo-finance – Jan Wilamowski Jul 26 '21 at 05:47
  • 1
    Does this answer your question? [How to fix new unable to read URL error in python for yahoo finance](https://stackoverflow.com/questions/68226884/how-to-fix-new-unable-to-read-url-error-in-python-for-yahoo-finance) – Stephen Ostermiller Feb 16 '22 at 11:19

2 Answers2

0

Try it like this.

from math import sqrt
from sklearn.cluster import MiniBatchKMeans 
import pandas_datareader as dr
from matplotlib import pyplot as plt
import pandas as pd
import matplotlib.cm as cm
import seaborn as sn

start = '2019-1-1'
end = '2020-1-1'

tickers = ['AXP','AAPL','BA','CAT','CSCO','CVX','XOM','GS','HD','IBM','INTC','JNJ','KO','JPM','MCD',    'MMM',  'MRK',  'MSFT', 'NKE','PFE','PG','TRV','UNH','RTX','VZ','V','WBA','WMT','DIS','DOW']
prices_list = []
for ticker in tickers:
    try:
        prices = dr.DataReader(ticker,'yahoo',start)['Adj Close']
        prices = pd.DataFrame(prices)
        prices.columns = [ticker]
        prices_list.append(prices)
    except:
        pass
    prices_df = pd.concat(prices_list,axis=1)
prices_df.sort_index(inplace=True)
prices_df.head()

enter image description here

ASH
  • 20,759
  • 19
  • 87
  • 200
-1

You can put the whole bunch of items in a single list. Yahoo Finance will retrive all those at once

import yfinance as yf
etf = ['AXP','AAPL','BA','CAT','CSCO','CVX','XOM','GS','HD','IBM','INTC','JNJ','KO'] 
tit = yf.download(tickers=etf, period='max')
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Simon
  • 117
  • 2
  • 10
  • How does this address the OP's question? Also please use the editing buttons or Markdown formatting rules to format your responses. Markdown hints may appear in a sidebar. Finally, please review topics in the help section (linked to at the top of every page) for additional guidelines about how to answer questions on SO. – SherylHohman Feb 21 '22 at 13:34