Problems appending data from loops

Hello! hope everyone’s keeping safe and well.

I’m creating a system to keep track of the performance of stocks over an extended period. What I intend for it to do is take tickers from a spreadsheet, search for those tickers on Yahoo finance, pull the historical data for those stocks and then stores the data against the ticker as dictionary or list. While I’ve got the webscrape working fine as well as attaching of the data against the ticker, I’m struggling to save the data within the for loop so I can access it later.

Here is the contents of spreadsheet:

Ticker    Name
WMT       Walmart
AMZN      Amazon
AAPL      Apple
CVS       CVS Health

After putting the contents of the sheet into a variable, I created the following series:

p_ticker = p_data['Ticker']

I have then set up a loop that runs through the sheet and applies the ticker from correctly.

for ticker in p_ticker:
    scrape_string = f"https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1=1480636740&period2=1606867140&interval=1d&events=history&includeAdjustedClose=true"
    ticker_data = {ticker:scrape_string}
    print(ticker_data)

However, for loops, dictionaries and lists are not particular areas of expertise and I would appreciate any advice available.

I’ve attempted the following:

for ticker in p_ticker:
    scrape_string = f"https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1=1480636740&period2=1606867140&interval=1d&events=history&includeAdjustedClose=true"
    ticker_data = []
    ticker_data.append = pd.read_csv(ticker)

This leads to an error saying: “FileNotFoundError: [Errno 2] File WMT does not exist: ‘WMT’”

If I try this:

for ticker in p_ticker:
    scrape_string = f"https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1=1480636740&period2=1606867140&interval=1d&events=history&includeAdjustedClose=true"
    ticker_data = []
    ticker_data.append = pd.read_csv(scrape_string)

I get “AttributeError: ‘list’ object attribute ‘append’ is read-only”

Unfortunately I don’t know where I’m going wrong… :frowning: :sob:

You’re getting that error b/c of how you’re using .append()
You don’t need an “=” with the append function.
https://docs.python.org/3/tutorial/datastructures.html

Though…I’m wondering if you could use [Pandas Datareader] for this project(https://readthedocs.org/projects/pandas-datareader/downloads/pdf/latest/) instead?

You could use Jupyter Notebook or Colab (or whatever you prefer) too.
Here’s an article about using datetime module in Python too.

Here’s a project that another learner posted where they use datareader and stocks.

What do you think?

Thanks for your points, though with points I managed to make a bit of progress by doing this

ticker_data = {}
for ticker in p_ticker:
    scrape_string = f"https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1=1480636740&period2=1606867140&interval=1d&events=history&includeAdjustedClose=true"
    ticker_data[ticker] = scrape_string

then if I write: ticker_data it outputs:

{'WMT': 'https://query1.finance.yahoo.com/v7/finance/download/WMT?period1=1480636740&period2=1606867140&interval=1d&events=history&includeAdjustedClose=true',

However I’d like to be able to add the name from the spreadsheet within the scrape string

I was thinking on this yesterday & I wondered why you need the spreadsheet at all? Rather than use the stock ticker symbols from the spreadsheet to feed into your code logic, would it be possible to write the function logic in the scraper itself to look for those 4 ticker symbols (or tags in the data) from the webpage and then save the data in a json (or whatever) file (in addition so using the time module with the sleep function to you can iterate over the page a certain number of times/day etc)? Are you using beautifulsoup by any chance?

I found this detailed article (maybe you already saw it) that might offer some insights.