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!!