Search code examples
pythonregexscreen-scrapingfinancestock

Google finance scraper return


I'm pretty new to python and programming in general. I'm currently working on a script to scrape stock quotes from Google finance. Here is my code:

import urllib.request as ur
import re

def getquote(symbol):
    base_url = 'http://finance.google.com/finance?q='
    content = ur.urlopen(base_url + symbol).read()
    m = re.search(b'id="ref_(.*?)">(.*?)<', content)
    if m:
        quote = m.group(2)
    else:
        quote = 'no quote available for: ' + symbol
    return quote

which returns:

b'655.65'

(655.65 is the current price of Google stock which is the symbol I passed in)

My question is: is there a way for me to either scrub the return so I just get the price without the b or the quotations? Ideally I'd like to have it returned as a float but if need be I can have it return as a string and convert it to a float when I need it later.

I've referenced these other posts:

How to create a stock quote fetching app in python

Python TypeError on regex

How to convert between bytes and strings in Python 3?

Convert bytes to a Python string

Perhaps I've missed something in one of those but I believe I've tried everything I could find and it is still returning in the format shown above.

SOLVED The problem I was having wasn't displaying a string without quotes, it was that I had a value set to a byte literal that needed to first be converted to a string literal and then to a float. I had tried this but I tried this outside of the if statement (noob move). the solution was as v1k45 suggested: add a line in the if statement quote = float(quote.decode('utf-8')) to decode it and convert to float.

thanks for the help!


Solution

  • Add a line in the if condition:

            quote = float(quote.decode('utf-8'))
    

    You have to decode the bytes to unicode to return a proper string. Use float() to convert it into a float.