Python Forum
Yahoo_fin question: how does one export all stock tickers to a csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Yahoo_fin question: how does one export all stock tickers to a csv file
#1
I noticed that the yahoo_fin script below from DustinKlent returns an error if a stock ticker is in the stock_list that is not in the yahoo_fin stock_info. To avoid error messages I would like to compare the list of stocks in yahoo_fin stock_info with my stock list in Excel before I run the below script. To be able to do this I need to export to csv all stock tickers in the yahoo_fin stock_info. Is that possible with Yahoo_fin? If yes, how? Please post the script.

import pandas as pd
from yahoo_fin import stock_info as si
import glob
  
stock_list = "ABEO", "ABUS"
 
stats = {}
  
for ticker in stock_list:
  data2 = si.get_stats(ticker)
  data2 = data2.iloc[:,:2]
  data2.columns = ["Attribute", "Recent"]
  stats[ticker] = data2
  
combined2 = pd.concat(stats)
combined2 = combined2.reset_index()
del combined2["level_1"]
combined2.columns = ["Ticker", "Attribute", "Recent"]
  
df = pd.DataFrame(combined2)
df2 = df.drop_duplicates(subset=None, keep="first", inplace=False)
df2.to_csv(r'stats.csv')
Reply
#2
For NASDAQ
ftp.nasdaqtrader.com
hover over product login and choose Market Data, daily list.
You will need to set up an account

You can get historical data from yahoo finance,
here's an app most of this is from Maik Rosenheinrich blog.
I only added class info:

MaikFetchStockData.py
# Note original code by: 
# Modified (slightly) by Larz60+, made into a class that cam be imported
# also added command line usage: python MaikFetchStockData.py symbol
# if run from command line without symbol, will ask for one
# returns a tab delimited csv file.
# you can also supply start and end dates, and reporting interval

import requests                  # [handles the http interactions](http://docs.python-requests.org/en/master/) 
from bs4 import BeautifulSoup    # beautiful soup handles the html to text conversion and more
import re                        # regular expressions are necessary for finding the crumb (more on crumbs later)
from datetime import datetime    # string to datetime object conversion
from time import mktime          # mktime transforms datetime objects to unix timestamps
import sys


class MaikFetchStockData:
    def __init__(self):
        pass

    def _get_crumbs_and_cookies(self, stock):
        """
        The first function calls the website of a selected stock and collects the cookies and crumb.
        We reuse the headers in the subsequent function to mimic the same browser.

        get crumb and cookies for historical data csv download from yahoo finance
        
        parameters: stock - short-handle identifier of the company 
        
        returns a tuple of header, crumb and cookie
        """
        
        # url = 'https://finance.yahoo.com/quote/{}/history'.format(stock)
        url = f"https://finance.yahoo.com/quote/{stock}/history"
        with requests.session():
            header = {'Connection': 'keep-alive',
                    'Expires': '-1',
                    'Upgrade-Insecure-Requests': '1',
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) \
                    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'
                    }
            
            website = requests.get(url, headers=header)
            soup = BeautifulSoup(website.text, 'lxml')
            crumb = re.findall('"CrumbStore":{"crumb":"(.+?)"}', str(soup))

            return (header, crumb[0], website.cookies)

    # 
    def convert_to_unix(self, date):
        """
        The second function just converts the provided start/stop dates into unix timestamps, because yahoo finance uses these in the request url. 

        converts date to unix timestamp
        
        parameters: date - in format (dd-mm-yyyy)
        
        returns integer unix timestamp
        """
        datum = datetime.strptime(date, '%d-%m-%Y')
        
        return int(mktime(datum.timetuple()))


    def load_csv_data(self, stock, interval='1d', day_begin='01-01-1991', day_end='02-11-2021'):
        """
        While the last function does the actual requesting of the historical stock data.
        This function returns a list of the individual daily values, containing the header (row 0) and subsequently one line per day. 
        
        queries yahoo finance api to receive historical data in csv file format
        
        parameters: 
            stock - short-handle identifier of the company
            
            interval - 1d, 1wk, 1mo - daily, weekly monthly data
            
            day_begin - starting date for the historical data (format: dd-mm-yyyy)
            
            day_end - final date of the data (format: dd-mm-yyyy)
        
        returns a list of comma seperated value lines
        """
        day_begin_unix = self.convert_to_unix(day_begin)
        day_end_unix = self.convert_to_unix(day_end)
        
        header, crumb, cookies = self._get_crumbs_and_cookies(stock)
        
        with requests.session():
            url = 'https://query1.finance.yahoo.com/v7/finance/download/' \
                '{stock}?period1={day_begin}&period2={day_end}&interval={interval}&events=history&crumb={crumb}' \
                .format(stock=stock, day_begin=day_begin_unix, day_end=day_end_unix, interval=interval, crumb=crumb)
                    
            website = requests.get(url, headers=header, cookies=cookies)
        
            return website.text.split('\n')[:-1]

    def get_csv_data(self, ticker='^DJI', interval='1d', day_begin='01-01-1991', day_end='02-11-2021'):
        return self.load_csv_data(ticker, interval='1d', day_begin='01-01-1991', day_end='02-11-2021')


def main(argv):
    getquote = MaikFetchStockData()
    print(f"length: {len(argv)}")
    if len(argv) == 1:
        stock = input("Please enter stock symbol: ")
    else:
        stock = argv[1]
    csvdata = getquote.load_csv_data(stock)
    print(csvdata)


if __name__ == '__main__':
    main(sys.argv)
sample output for python MaikFetchStockData.py ^DJI > DJI.csv
Output:
['Date,Open,High,Low,Close,Adj Close,Volume', '1992-01-02,1357.599976,1357.619995,1330.900024,1342.099976,1342.099976,41400', '1992-01-03,1342.300049,1354.630005,1342.300049,1350.699951,1350.699951,45900', '1992-01-06,1350.699951,1366.219971,1349.219971,1365.500000,1365.500000,48800', '1992-01-07,1365.500000,1383.030029,1361.550049,1383.000000,1383.000000,57000', ... '2021-02-05,12727.879883,12833.669922,12727.879883,12788.509766,12788.509766,852300', '2021-02-08,12793.919922,12894.870117,12782.839844,12874.919922,12874.919922,1231400', '2021-02-09,12871.200195,12993.620117,12780.129883,12959.900391,12959.900391,830100', '2021-02-10,12976.240234,13041.500000,12885.309570,12954.530273,12954.530273,809300']
to use in your own program,

from MaikFetchStockData import MaikFetchStockData

class myclass:
    def __init__(self):
        self.fetch_stockdata = MaikFetchStockData().get_csv_data

    def get_stuff(self, symbol):
        return self.fetch_stockdata(symbol)

if __name__ == '__main__':
    mc = myclass()
    print(mc.get_stuff('MSFT'))
Reply
#3
Thanks for the reply. I ran your program and understand the output. That is not what I was asking for.

In the Yahoo_fin script I posted above the script line below imports stock_info. The subsequent script then extracts various information from the imported stock_info and exports them. More specifically it exports for each ticker the values of 59 stat types to csv.

from yahoo_fin import stock_info as si
So far so good. That is working fine and is very useful.

What I am looking for is a script that exports the ticker symbols only from the imported stock_info. Exports all tickers symbols in Stock_info, without having to enter the ticker names in the script. I would like to have a script that results in the below csv output. I only list a few tickers to illustrate. Presumably the output will contain several hundred tickers. So, I want to dump all tickers symbols in stock_info to csv.

Output:
AACG AACQU AACQW AAL AAME AAOI AAON AAPL AAWW AAXJ
On the Yahoo_fin documentation page, see link below, I found links to the websites that have the ticker symbols.
http://theautomatic.net/yahoo_fin-documentation/
Look at explanation for tickers_nasdaq(), tickers_dow(), tickers_nifty50(), tickers_other(), tickers_sp500()

I manually extracted the ticker symbols from these websites, compiled them to one list in Excel and compared them to my own ticker list. That way I was able to find the tickers that are no longer listed and not contained in stock_info. Tickers that are not in stock_info cause the above yahoo_fin script to return an error message. I ran into this problem when I ran the above yahoo_fin script with my several hundred tickers. After I had the unlisted tickers removed from my my ticker list I did not get an error message any longer and was able to export the stats for several hundred tickers at once.

Exporting the ticker symbols manually from several websites is a workaround to solve my problem. However, easier would be if I could just dump the ticker symbols from stock_info. I don't know if that can be done.

Maybe the tickers can be exported using tickers_nasdaq(), tickers_dow(), tickers_nifty50(), tickers_other(), tickers_sp500(). I don't know. I am a newbie to yahoo_fin.
Reply
#4
To get symbols:

I wrote this some time ago, but it worked at the time.
You might have to adjust a few things
but at the time, I believe it got all the symbols for NYSE, NASDAQ and AMEX

since AMEX is now NYSE American you will have to fix that

https://github.com/Larz60p/PySymbol

There was a script named TrySymbolFetch.py that would locate company by symbol name or symbol by company name included.
Reply
#5
Thanks for your information. I had a look at it. Mmm. Considering that my skills in python are very limited it is easier for me to stick with the ticker info provided on the NASDAQ website (ftp://ftp.nasdaqtrader.com/SymbolDirectory/). The files "nasdaqlisted.txt" and "otherlisted.txt" contain all the ticker symbols I need. That is NASDAQ and AMEX. AMEX tickers are included in the "otherlisted.txt" file. I can extract the tickers from these text files with an Excel VBA.

However, my initial question has still not been answered. Is it possible to export ticker symbols from yahoo-fin to csv?

Yahoo_fin contains a function called "tickers_nasdaq()". The description for this function says: "Returns a list of tickers currently listed on the NASDAQ." This is exactly what I want. I just don't know how to script it so that the ticker list is pulled and exported to csv. It should be a no-brainer for anybody with yahoo_fin scripting experience. If you know how to script it then please post it here.
Reply
#6
I found the solution. The below yahoo_fin script will export all ticker symbols in the NASDAQ to a csv file.

import pandas as pd
from yahoo_fin import stock_info as si
 
data = si.tickers_nasdaq()

df = pd.DataFrame(data)
df2 = df.drop_duplicates(subset=None, keep="first", inplace=False)
df2.to_csv(r'stats.csv')
Reply
#7
The below yahoo_fin script will export 2 ticker lists to 2 csv files.
Question: how do I need to script so that the data is combined and exported to 1 csv file?

import pandas as pd
from yahoo_fin import stock_info as si
 
data1 = si.tickers_nasdaq()
data2 = si.tickers_other()

df = pd.DataFrame(data1)
df2.to_csv(r'NASDAQ_tickers.csv')

df = pd.DataFrame(data2)
df2.to_csv(r'Other_tickers.csv')
Reply
#8
I now also figured out how to combine two data sets. See below script. With that my questions regarding exporting stock ticker symbols and combining data sets are answered.
This post can be marked as: Solved

import pandas as pd
from yahoo_fin import stock_info as si
 
data1 = si.tickers_nasdaq()
data2 = si.tickers_other()

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

df = pd.concat([df1, df2])
df.to_csv('Combined.csv')
Reply
#9
Stock tickers are returned as a list. So there is no need to use pandas if only requirement is to write tickers into file, vanilla Python is good enough.

Following code creates combined list of tickers and writes list into file so that every ticker is on separate line:

tickers = [*si.tickers_nasdaq(), *si.tickers_other()]

with open('tickers.csv', 'w') as f:
    print(*tickers, sep='\n', file=f)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#10
Learned something new. Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exporting Stock Fundamental Data to a CSV file with yahoo_fin DustinKlent 2 4,726 Aug-01-2022, 06:08 PM
Last Post: paulyan
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,164 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,785 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Skeleton file export error Python Code pepapoha 4 3,498 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Python Export Question Below samlee916 1 1,575 Jul-07-2020, 12:22 PM
Last Post: Larz60+
  export file and display cmd prompt same time jacklee26 1 2,031 Jul-24-2019, 05:15 AM
Last Post: Larz60+
  [PYTHON EXPORT SQL TO .txt file] PYTHONDUDE 9 24,296 Feb-21-2018, 08:54 PM
Last Post: nilamo
  Need help to open PDF file and Export to text file ratna_ain 3 7,065 Oct-10-2017, 01:44 AM
Last Post: ratna_ain

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020