简体   繁体   中英

Python Get Stock Prices

I am working on a tutorial to pull different stock prices from Yahoo Fiances. I have this code which works but prints out the sentence the price of the different stock symbols and then the array brackets but not the prices. All help greatly appreciated.

import urllib
import re

symbolslist = ["aapl", "spy", "goog", "nflx"]

i = 0
while i < len(symbolslist):
    url = "http://finance.yahoo.com/q?s=" + symbolslist[i] + "&ql=1" 
    htmlfile = urllib.urlopen(url)
    htmltext = htmlfile.read()
    regex = '<span id="yfs_l84_' + symbolslist[i] + ' "> (.+?) </span>'
    pattern = re.compile(regex)
    price = re.findall(pattern, htmltext)
    print "the price of ", symbolslist[i], " is ", price
    i += 1

EDIT: on second thought, your regexp is not matching anything at all; please check if the regexp is correct to start with.

EDIT2: OK, looks like you're putting whitespace where it doesn't need to be (and not putting it where it should be). Please try to be tidier in the future when it comes to formatting your code, both for your own use but especially when showing to other people. The correct regexp is:

regex = '<span id="yfs_l84_' + symbolslist[i] + '">(.+?)</span>'

Other than that...

The simplest fix would be changing this

price = re.findall(pattern, htmltext)

to

price = re.findall(pattern, htmltext)[0]

because re.findall returns a list, not a single item, and the string representation of a list is [bla, bla, bla, ...] .

Furhtermore , in order to loop over a sequence of items, don't use a while loop with a manual counter and indexing—this is not assembler:

for symbol in symbols:
    ...

也许'“>之间的空格导致了问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM