So I just started programming a few hours ago, working on an example from Nathan Yau's Visualize This. I'm using Python 3.3.4 and BeautifulSoup4 for my first data scrape exercise. Though the book seems to be using Python 2.x, I've managed to figure out the updated codes to complete my first data scraping exercise using historical data from wunderground.com:
>>> maxTemp = soup.findAll(attrs={"class":"nobr"})[5].span.string
>>> print(maxTemp)
The result, as it should be, is "37."
I included this bit of code in my first script and when I tried to run it from the command prompt, it starts but then I get the error:
"AttributeError: 'NoneType' object has no attribute 'string'"
As you can imagine, it's frustrating to see my code work fine in the Python IDLE GUI and not from the script. I've looked around for answers and tried different things, but am now definitively stuck. Any suggestions?
EDIT: Adding more code for my example. This is from the script that fails:
url = "http://www.wunderground.com/history/airport/KBOS/2013/" + str(m) + "/" + str(d) + "DailyHistory.html"
page = urllib.request.urlopen(url)
# Get daily maximum temperature from page
soup = BeautifulSoup(page)
# maxTemp = soup.body.nobr.b.string
maxTemp = soup.findAll(attrs={"class":"nobr"})[5].span.string
Again, it fails when I run it from my terminal:
C:\Python33>python get-weather-data.py
Getting data for 201311
Traceback (most recent call last)
File "get-weather-data.py", line 28, in (module)
maxTemp = soup.findAll(attrs={"class":"nobr"})[5].span.string
AttributeError: 'NoneType' object has no attribute 'string'
Even though it works fine here in IDLE:
import urllib.request
page = urllib.request.urlopen("http://www.wunderground.com/history/airport/KBOS/2013/1/1/DailyHistory.html")
from bs4 import BeautifulSoup
soup = BeautifulSoup(page)
maxTemp = soup.findAll(attrs={"class":"nobr"})[5].span.string
print(maxTemp)
.stringfromsoup.findAll(attrs={"class":"nobr"})[5].span, but that latter is set toNone, not to whatever you're expecting. Why this is the case, is difficult to say. You should post a full example, instead of just one line.