[Tutor] Python3: looping through web address

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jul 18 13:48:11 EDT 2016


On 18/07/16 15:10, Umed Saidov wrote:

> No. nothing clever I am afraid. I wanted to create a variable to store 
> data in pandas format. This seemed like a good way of doing it... but 
> perhaps not.
>>> #open a cvs file with 100+ stock tickers from S&P500. Save in a
>>> dataframe 'ticker'.
>>> ticker =pd.read_csv('tickers.csv')
>>
> the csv is just a column of tickers. I wanted to store them in pandas 
> format. this returns a pandas dataframe with contents of the csv file 
> stored in two columns (index and ticker values)

In that case you should be able to miss out the two lines above and just use

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

>      I want the content of the column one in ticker. With each iteration 
> of t, which I thought was just a simple numeric progression representing 
> a row number, I wanted to get the content of the column one.

In Python a for loop returns the actual data items not
an index. So I would expect the value of t to be one of
whatever ticker.itertuples() returns - presumably a tuple.
And if you want the first(only?) element then you need to use


t[0] to get it.

>> 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)
>>
> Thanks for the suggestion. I tried this and received the badstatusline 
> error message:

OK, In that case comment out the openurl() line and just look at what
values the address has (or even just t itself initially). Simplify as
much as possible. Once you know you are getting the right value then you
can create the right address, then you can try to open it.

Take it one step at a time.
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list