How to Download a CSV and Consume it

Recently, I learn Python to download csv data from the Nasdaq Stock Screener page,

There are some ways to implement it. Here I just show one way. I will use time, datetime, and pandas packages to do it.

					import time
import datetime as dt
import pandas as pd
from pandas.errors import EmptyDataError

# Debugging
import logging

log = logging.getLogger(__name__)

def read_all_stock_symbols():
    df = pd.read_csv('assets/all-stock-symbols.csv', usecols=['Symbol'])
    return df

def download_yahoo_symbol_csv():
    all_symbols = read_all_stock_symbols()
    start_date = int(time.mktime(( - dt.timedelta(days=1 * 365)).timetuple()))
    end_date = int(time.mktime(
    interval = '1d'  # 1d, 1m, 1yr
    for index, row in all_symbols.iterrows():
        symbol = row['Symbol']
        api_url =
            df = pd.read_csv(api_url)
        except EmptyDataError as e:
            log.debug('Cannot download: %s. Error details: %s', api_url, e)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

How Does It Work

pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language. pandas.read_csv is the key method to return DataFrame type. With the parameter [‘Symbol’], it will return the Symbol column data only.

To fetch the DataFrame type data, we can use its iterrows() method to yield both the index and row. The line 22 shows fetching each symbol and generate a URL, which can get each symbol’s historical data from the

To read the CSV, just use the read_csv() method to get DataFrame type data.

Error Handling

Generally, programmers cannot remember all kinds of exceptions that might occur in a complex operation, so they like to catch all exceptions. Therefore, it is also a good way to write undebugable code if you are not careful.

It is very important to record the actual cause of the exception somewhere for choosing to catch all exceptions(for example, log files, error messages printed to the screen, and so on)

Consider this example:

					def parse_int(s):
        n = int(v)
    except Exception:
        print("Couldn't parse")

If you try this function, it behaves like this:

>>> parse_int(‘n/a’)

Couldn’t parse

>>> parse_int(’42’)

Couldn’t parse


The code example is from this book: Python Cookbook: Recipes for Mastering Python 3, 3rd Edition

Hits: 15

Leave a Reply

Your email address will not be published. Required fields are marked *