[Tutor] Python3: looping through web address

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jul 18 07:35:03 EDT 2016

On 18/07/16 03:00, Umed Saidov wrote:

> import urllib.request, json
> import csv
> import pandas as pd
> import itertools
> ticker = []
> ticker = pd.DataFrame(ticker)

Caveat: I'm not a Pandas expert but...
This looks odd. You first assign an empty list to ticker and pass that
into pandas.DataFrame then assign the result back to ticker. Is that
what you intended? Why not just

ticker = pd.DataFrame([])

Or is there something clever going on?

> #open a cvs file with 100+ stock tickers from S&P500. Save in a 
> dataframe 'ticker'.

> ticker =pd.read_csv('tickers.csv')

And now you overwrite the dataframe with the content of a csv file?
I'm not sure what read_csv returns but it seems to me you are
creating and throwing away a lot of stuff here?

> for t in ticker.itertuples():
>      response += 
> urllib.request.urlopen('https:websiteaddress/{ct}/financials'.format(ct=ticker))

ticker is the iterable, it does not change during the loop.
Yet in the url it is ticker you assign to the ct marker.
Don't you want 't'? That's the thing that changes.

It might help to create and print the address before calling urlopen:

for t in ticker.itertuples():
    address = 'https:websiteaddress/{ct}/financials'.format(ct=ticker)
    print address   # remove when done
    response += urllib.request.urlopen(address)

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list