bytes-like Object is required, not 'str'

Asked

Viewed 5,950 times

1

I’m trying to run this code:

import time

import urllib.request

from urllib.request import urlopen

ibov = ['BBAS3.SA', 'PETR4.SA']

def yahooKeyStats(stock):

try:
    sourceCode = urllib.request.urlopen ('https://finance.yahoo.com/quote/.../key-statistics?p=' +stock).read()
    pl = sourceCode.split('Preço/Livro </td><td class="Fz(s) Fw(500) Ta(end)">')[1].split('</td>')[0]
    print ("preço livro:", pl)

except Exception as e:
       print (e)

I have already checked all the possibilities and I can not correct. Excuse me the question, that may be silly, but I am starting and I have doubts.

1 answer

2


Your problem is quite simple. If you check the urlopen returns an object of type "bytes" convert "sourceCode" to string first:

sourceCode = str(sourceCode)
pl = sourceCode.split('Preço/Livro </td><td class="Fz(s) Fw(500) Ta(end)">')[1].split('</td>')[0]
print ("preço livro:", pl)

Another thing and what you are doing to extract the stock price is a little confusing recommend you use the Beautifulsoup to extract information from an HTML document.

  • thanks. apologies for not responding before.

  • @Baco23 if the problem has been solved, do not forget to accept the answer to mark it as solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.