0

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!

1
  • Did you try to change the line print "the price of",symbolslist[i]," is " ,price? Commented Nov 4, 2017 at 23:50

1 Answer 1

1

Try this:

import urllib
import re

symbolfile = open("symbols.txt")

symbolslist = symbolfile.read()

symbolslist = symbolslist.split("\n")

with open('output.csv', 'w') as output:
    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)
        output.write('%s,%s\n'%(symbolslist[i], price[0]))
        print "the price of",symbolslist[i]," is " ,price
        i+=1
Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to print these results with a newline for each row? They appear to be in a single row for now.
Nevermind. Figured it out. output.write('%s,%s'%(symbolslist[i], price[0]+"\n"))
Yeah, I forgot that, sorry :-)
I corrected the answer. It is better to place the "\n" inside the initial string
I'm still a noob. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.