When running the following code the split function is causing an error
def yahooKeyStats(stock): try: sourceCode = urllib2.urlopen('http://finance.yahoo.com/q/ks?s='+stock).read() pbr = sourceCode.split('Price/Book (mrq):</td><td class="yfnc_tabledata1">')[1].split('</td>')[0] print 'price to book ratio:',stock,pbr
except Exception,e: print 'failed in the main loop',str(e)
I am getting this error - failed in the main loop a bytes-like object is required, not 'str'
I am using python 3.5
Any help will be much appreciated
Cheers
You must be logged in to post. Please login or register an account.
If you were on python 3+, those print statements would cause a syntax error, also urllib2 isn't a thing. You sure you are really using Python 3 here?
It's possible you copied that code from the tutorial and pasted here instead, but, if that's the case, you need to decode the data. Python 2 treated bytes and strings the same. Python 3 separates those, so you need to convert bytes to strings. Apply a .decode() to the end of the source code you pull.
-Harrison 10 years ago
You must be logged in to post. Please login or register an account.
Hi Yes i used urlib and made the syntax changes tor print and exception
import time import urllib from urllib.request import urlopen
def yahooKeyStats(stock): try: sourceCode = urllib.request.urlopen('http://finance.yahoo.com/q/ks?s='+stock).read() pbr = sourceCode.split('Price/Book (mrq):</td><td class="yfnc_tabledata1">')[1].split('</td>')[0] print ('price to book ratio:',stock,pbr)
except Exception as e: print ('failed in the main loop',str(e))
yahooKeyStats('a')
I added a .decode() to the end of the sourceCode and still get an error 'list index out of range'
I might not be using the .decode() properly any guidance will be appreciated
Cheers
-SGaud1 10 years ago
You must be logged in to post. Please login or register an account.
Yeah I'm having the same problem with regards to the "list index out of range".
-petermcquaid 9 years ago
You must be logged in to post. Please login or register an account.