My goal is to capture the stock name (symbol) and the stock price. I am able to print the results, but I am not sure how to save these results to a text/csv file. The values in my symbols.txt file are:
PIH
TURN
FLWS
FCCY
SRCE
VNET
TWOU
The script I am currently running is:
import urllib
import re
symbolfile = open("symbols.txt")
symbolslist = symbolfile.read()
symbolslist = symbolslist.split("\n")
i=0
while i<len(symbolslist):
url = "http://www.nasdaq.com/symbol/" +symbolslist[i]
htmlfile = urllib.urlopen(url)
htmltext = htmlfile.read()
regex = '<div id="qwidget_lastsale" class="qwidget-dollar">(.+?)</div>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print "the price of",symbolslist[i]," is " ,price
i+=1
My current output is:
the price of PIH is ['$7.175']
the price of TURN is ['$2.03']
the price of FLWS is ['$9.45']
the price of FCCY is ['$18']
the price of SRCE is ['$50.87']
the price of VNET is ['$7.145']
the price of TWOU is ['$63.89']
My desired output would be a text/csv file with the following values:
PIH,$7.175
TURN,$2.03
FLWS,$9.45
FCCY,$18
SRCE,$50.87
VNET,$7.145
TWOU,$63.89
Dollar signs, brackets, ect are fine. I can remove those in another program. Thanks in advance!
print "the price of",symbolslist[i]," is " ,price?