1

Here is the code for Python 3 for web scraping Yahoo finance stock price of AAPL.

import urllib.request
from bs4 import BeautifulSoup as bs4

htmlfile = urllib.request.urlopen("http://finance.yahoo.com/q?s=AAPL")

htmltext = htmlfile.read()

for price in htmltext.find(attrs={'id':"yfs_184_aapl"}):
    print (price)

Apparently, the code works fine with little modification in Python 2.7. However, it does not work in Python 3.3.3 Shell. Here is the error it shows:

Traceback (most recent call last):
  File "C:/Python33/python codes/webstock2.py", line 8, in <module>
    for price in htmltext.find(attrs={'id':"yfs_184_aapl"}):
TypeError: find() takes no keyword arguments

I have learned to correct the string pattern to binary via str.encode. I'm not sure this I can work with this code.

Edit1: Final working code change after @Martijn

    import urllib.request
    from bs4 import BeautifulSoup as bs4

    htmlfile = urllib.request.urlopen("http://finance.yahoo.com/q?s=AAPL")

    htmltext = htmlfile.read()

    soup = bs4(htmltext)

    for price in soup.find_all(id="yfs_l84_aapl"):
        print (price)

It prints out blank. Could you figure this out. thanks again.

L.fole
  • 687
  • 3
  • 12
  • 19
  • 2
    Better yet, get the quote in CSV and skip screen-scraping altogether: http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&f=sl1 . More details: http://www.gummy-stuff.org/Yahoo-data.htm – Jimothy Jan 02 '14 at 15:38

1 Answers1

3

You are calling str.find(), not BeautifulSoup.find(). You forgot something:

soup = bs4(htmltext)

for price in soup.find(attrs={'id':"yfs_184_aapl"}):

But if you are going to loop, you need to call find_all(), really:

for price in soup.find_all(id="yfs_l84_aapl"):

You don't actually have to use the attrs keyword argument; specifying the attributes as keyword arguments directly works fine too.

You do have to use the correct id attribute; it is yfs_l84_aapl (letter l, followed by the digits 8 and 4), not the digit 1.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I have updated (check the code inserted in edit in the question). Unfortunately, it provide neither error nor any output value. Just blank. – L.fole Jan 02 '14 at 15:37
  • Oops. Silly mistake. Thanks a lot for pointing it out. I have been scratching my head all over. :) – L.fole Jan 02 '14 at 15:40