Nonetype object has no attribute 'b'

426 views
Skip to first unread message

ash1ey

unread,
Feb 12, 2009, 10:56:01 AM2/12/09
to beautifulsoup
I am trying to collect weather data from a website called Weather
Underground using Beautiful Soup. I found the code necessary to do
this from a website called Flowing Data. However, one line of the code
is giving me trouble. The code is as follows:

import urllib2
from BeautifulSoup import BeautifulSoup

# Create/open a file called wunder.txt (which will be a comma-
delimited file)
f = open('wunder-data.txt', 'w')

# Iterate through year, month, and day
for y in range(1980, 2007):
for m in range(1, 13):
for d in range(1, 32):

# Check if leap year
if y%400 == 0:
leap = True
elif y%100 == 0:
leap = False
elif y%4 == 0:
leap = True
else:
leap = False

# Check if already gone through month
if (m == 2 and leap and d > 29):
continue
elif (m == 2 and d > 28):
continue
elif (m in [4, 6, 9, 10] and d > 30):
continue

# Open wunderground.com url
url = "http://www.wunderground.com/history/airport/KBUF/"+str(y)
+ "/" + str(m) + "/" + str(d) + "/DailyHistory.html"
page = urllib2.urlopen(url)

# Get temperature from page
soup = BeautifulSoup(page)
dayTemp = soup.body.nobr.b.string

# Format month for timestamp
if len(str(m)) < 2:
mStamp = '0' + str(m)
else:
mStamp = str(m)

# Format day for timestamp
if len(str(d)) < 2:
dStamp = '0' + str(d)
else:
dStamp = str(d)

# Build timestamp
timestamp = str(y) + mStamp + dStamp

# Write timestamp and temperature to file
f.write(timestamp + ',' + dayTemp + '\n')

f.close()


The line that is giving me trouble is "dayTemp =
soup.body.nobr.b.string"
It gives me the following error message:
"Traceback (most recent call last):
File "C:/Python24/whatever2", line 36, in -toplevel-
dayTemp = soup.body.nobr.b.string
AttributeError: 'NoneType' object has no attribute 'b' "

Can anyone give insight into what the problem is? It would also be
helpful if anyone could tell me what "b" is referring to.

I appreciate it!
ash1ey

Aaron DeVore

unread,
Feb 12, 2009, 2:42:02 PM2/12/09
to beauti...@googlegroups.com
First I'm going to expand the line soup.body.nobr.b.string so it's
clear what they're doing. BeautifulSoup provides some wonderful
shortcuts but it's helpful to expand them when explaining things.

body = soup.find("body")
nobr = body.find("nobr")
b = nobr.find("b")
dayTemp = b.contents[0]

The problem is at line 2. There is no element named nobr in body so
the variable nobr is None. None doesn't have b as an attribute, hence
the AttributeError. I don't have the HTML in front of me so I don't
know what needs to be changed, but hopefully that gets you moving in
the right direction.

-Aaron

ash1ey

unread,
Feb 13, 2009, 2:40:52 PM2/13/09
to beautifulsoup
I have been working on this code all day, trying to figure out what
should be in my code instead of "body." I have been looking at the
HTML, and it appears that they have changed their website, so the code
is no longer correct. Near the number I need BS to grab, there is
"nobr" and "b", so I think "body" is the wrong element here. If anyone
could look at the HTML and give me some direction about what word I
need to use instead of "body", I would greatly appreciate it!

ash1ey

On Feb 12, 2:42 pm, Aaron DeVore <aaron.dev...@gmail.com> wrote:
> First I'm going to expand the line soup.body.nobr.b.string so it's
> clear what they're doing. BeautifulSoup provides some wonderful
> shortcuts but it's helpful to expand them when explaining things.
>
> body = soup.find("body")
> nobr = body.find("nobr")
> b = nobr.find("b")
> dayTemp = b.contents[0]
>
> The problem is at line 2. There is no element named nobr in body so
> the variable nobr is None. None doesn't have b as an attribute, hence
> the AttributeError. I don't have the HTML in front of me so I don't
> know what needs to be changed, but hopefully that gets you moving in
> the right direction.
>
> -Aaron
>

Jen Jack Gieseking

unread,
Sep 28, 2013, 9:19:53 PM9/28/13
to beauti...@googlegroups.com
The error here was this line of code (dayTemp = soup.body.nobr.b.string) is a comment and should not have been entered int he get-weather-data.py file.
Good luck,
JJG
Reply all
Reply to author
Forward
0 new messages