0
import urllib.request, urllib.error

m = 0 
web ='x'  # This reads the stock value for "United States Steel Corp."
t =str(web)
try: f = urllib.request.urlopen('http://finance.yahoo.com/q?s='+ t +'')
except ValueError:
    print(str('Error'))
    m = 1
    pass

if m == 0:
    urlText = f.read().decode('utf-8')

    if urlText.find('<span id="yfs_l84_'+ t +'">'):
        cat = urlText[urlText.find('<span id="yfs_l84_'+ t +'">'):urlText.find('<span id="yfs_l84_'+ t +'">')+30]
        dog = cat.strip('</span></span>')
        dog = cat.strip('<span id="yfs_l84_'+ t +'">')
        print('United States Steel Corp. = ', dog)
    else:print("---> Couldn't read URL text")

This program is reading the stock value for the specific company's abbreviation. In my case line 3, where it says web ='x'

What I want to achieve is that, if I type in more abbreviations in that assigned web variable, then I should be able to display the stock values for all those entered abbreviations. What I mean is this: web = 'x', 'y', 'z'.

I am not sure how to implement that in my program. I believe that I will need to create an array and then loop through using a for loop. However, I am unsure of it.

Thanks!!

Onilol
  • 1,315
  • 16
  • 41
krazzy
  • 179
  • 1
  • 1
  • 10
  • I think your `web` var is already of `type string` so that `t` var would be unnecessary. – Onilol Sep 22 '15 at 20:19
  • 1
    use a `for` loop/iterator. but a good approach would be rewrite your code in a function. takes the argument t and fetches it's stock value. then, use a for iterator - like @Onilol showed. `for t in web: your_function(t)` – marmeladze Sep 22 '15 at 20:22
  • exactly ! It is better to separate your program in different tasks ( functions ) ! Each specific thing could be a function ! That increases your code quality and makes it easier to maintain as @marmeladze suggested. – Onilol Sep 22 '15 at 20:24
  • See here for easier ways to get this information http://stackoverflow.com/questions/27543776/yahoo-finance-webservice-api – kichik Sep 22 '15 at 20:49
  • Is their anyone who could please help me further with this program. I did get correct results from what Onilol gave me initially. Now, I just want to read x , y , z from web = ['x','y','z'] with its corresponding stock value. It should display -> x = stockvalue, y = stockvalue, z = stockvalue. – krazzy Sep 22 '15 at 23:15

2 Answers2

1

Arrays in Python are called lists! You you could do something like:

web = ['x','y','z']

and to loop through them you could something like :

for i in web:
    #do stuff

Also this link may help you learn Python : Python Track in CodeAcademy

EDIT: OP is looking for a dictionary instead.

companies = {
        'Company A' : 15,
        'Company B' : 6 
        }

in a dictionary you can access elements by their index : companies['Company A'] would return it's value 6

Onilol
  • 1,315
  • 16
  • 41
  • Change `i` to `t` and then the rest of the code won't need to change :) – l'L'l Sep 22 '15 at 20:22
  • if his `'x','y','z'` are already strings the call to `t` would be unnecesary ! Depends mostly on what type of data he is dealing with. – Onilol Sep 22 '15 at 20:26
  • Okay. Great. That worked. Next question is how do I read the company name. For ex: for 'x', the company name is "United States Steel Corp.", so when I get my stock values, it should print "United States Steel Corp. = stockvalue(a number)". That is for every company name I enter. – krazzy Sep 22 '15 at 20:37
  • No. I cannot. We need to transfer that same company name abbreviation to be printed out with the stock value. So, no matter how many abbreviations I enter, it should read the company name abbreviation that is in web = ['x','y','z']. So, x = stockvalue, y = stockvalue, z = stockvalue, and so on if I enter more – krazzy Sep 22 '15 at 20:50
  • In that case what you want is not a list. what you want is a dictionary. I'll edit my answer to give you an example of a dictionary. – Onilol Sep 22 '15 at 20:51
  • You might also want to look at web scraping to automate your task if you don't have access to an API – Onilol Sep 22 '15 at 20:56
  • I just want to read x , y , z from web = ['x','y','z'] with its corresponding stock value such x = stockvalue. – krazzy Sep 22 '15 at 21:06
0

You request quotes via the YFinance API by joining symbols with + (e.g. AAPL+GOOG+MSFT). To join a list of symbols like this, '+'.join(symbols). You then need to append with &f=.... I've used &f=a to get the Ask price.

import urllib

symbols = ['MSFT', 'AAPL', 'GOOG']

# To request ask (delayed) for all symbols.
url_req = ("http://finance.yahoo.com/d/quotes.csv?s={symbols}&f=a"
           .format(symbols="+".join(symbols)))

prices = urllib.request.urlopen(url_req).read().decode().split('\n')

# Use dictionary comprehension together with zip to get dict of values.
ask_quotes = {symbol: quote for symbol, quote in zip(symbols, prices)}

>>> url_req
'http://finance.yahoo.com/d/quotes.csv?s=MSFT+AAPL+GOOG&f=a'

>>> ask_quotes
{'AAPL': '114.60', 'GOOG': '614.920', 'MSFT': '43.93'}
Alexander
  • 105,104
  • 32
  • 201
  • 196