3

Say I have use

date = r.find('abbr')

to get

<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>

I just want to print November 16, 2012, but if I try

print date.string

I get

AttributeError: 'NoneType' object has no attribute 'string'

What am I doing wrong?

UPDATE: Here's my code Neither of the print pairs print the raw string, but the uncommented ones get the correct tags

from BeautifulSoup import BeautifulSoup
page = urllib2.urlopen("some-url-path")
soup = BeautifulSoup(page)
calendar = soup.find('table',{"class" : "vcalendar ical"})
for r in calendar.findAll('tr'):
#   print ''.join(r.findAll('abbr',text=True))
#   print ''.join(r.findAll('strong',text=True))
    print r.find('abbr')
    print r.find('strong')
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
kevlar1818
  • 3,055
  • 6
  • 29
  • 43

2 Answers2

3

soup.find('abbr').string should work fine. There must be something wrong with date.

from BeautifulSoup import BeautifulSoup

doc = '<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>'

soup = BeautifulSoup(doc)

for abbr in soup.findAll('abbr'):
    print abbr.string

Result:

November 16, 2012

Update based on code added to question:

You can't use the text parameter like that.

http://www.crummy.com/software/BeautifulSoup/documentation.html#arg-text

text is an argument that lets you search for NavigableString objects instead of Tags

Either you're looking for text nodes, or you're looking for tags. A text node can't have a tag name.

Maybe you want ''.join([el.string for el in r.findAll('strong')])?

Acorn
  • 49,061
  • 27
  • 133
  • 172
  • Just don't use `text=True` when searching for elements. Find the elements you want and then use the `.string` property of those elements. – Acorn Nov 11 '11 at 03:17
0

The error message is saying that date is None. You haven't shown enough code to say why that is so. Indeed, using the code you posted in the most straight-forward way should work:

import BeautifulSoup

content='<abbr class="dtstart" title="2012-11-16T00:00:00-05:00">November 16, 2012</abbr>'
r=BeautifulSoup.BeautifulSoup(content)
date=r.find('abbr')
print(date.string)
# November 16, 2012
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677