Reputation: 807
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.
Upvotes: 1
Views: 3148
Reputation: 1121168
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
.
Upvotes: 3