Bytes to String or String to Bytes?
Question: I have read some of the examples here but I am such a novice I don't
understand some of them and others don't seem to work(probably because I'm
such a novice but...

    
    
    import urllib.request
    import re
    Symbols = ['aapl', 'spy' , 'goog' , 'nflx']
    i = 0
    while i < len(Symbols):
        Yahoo='http://finance.yahoo.com/q?s=' + Symbols[i]
        htmlfile = urllib.request.urlopen(Yahoo)
        htmltext = htmlfile.read()
        string = Symbols[i]
        symbol = string.encode('utf-8')
        pattern= re.compile(b'<span id="yfs_l84_'+ symbol +'">(.+?)</span>')
        price= re.findall(pattern, htmltext)
        print('The price of' + str(Symbols[i]) + ' is ' + str(price))
        i+=1
    

This doesn't work because in the `re.compile` statement I am trying to
concatenate str and bytes.

I need to convert the string to bytes so that I can later iterate over a list
of symbols and scrape the latest stock price from yahoo finance.

I have a feeling there is something wrong with my syntax there and the
examples and python documentation has an argument for 'encoding' which I think
is the 'utf-8' string but I don't really know.

Can someone help me with this?

Edit: I am using Bytes here because that is the only way it works, I get an
error to change it to bytes if I don't (I am using 3.3).

The error is this:

    
    
    Traceback (most recent call last):
      File "C:\Users\Deaven And Teigan\Documents\Python Projects\YahooFinance.py", line 14, in <module>
        pattern= re.compile(b'<span id="yfs_l84_'+ symbol +'">(.+?)</span>')
    TypeError: can't concat bytes to str
    


Answer: You should work with strings right up to the point where you actually want to
make the network request rather than mix strings and bytes in your code.
Generally eating, strings are the abstract representation of characters,
whereas bytes are a specific encoding (e.g. Utf-8) of a string into a sequence
of bytes that can be sent over a network.

Maybe you want to use a raw string for this line:

    
    
    pattern= re.compile(b'<span id="yfs_l84_'+ symbol +'">(.+?)</span>')
    

Instead use

    
    
    r'<span id="yfs_l84_'
    

