Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird syntax error in using beautifulsoup.find()

This may be entirely obvious, but I'm stumped (kinda new to python, sorry):

page = urllib2.urlopen("http://www.somerandompage.com")
soup = BeautifulSoup(page)
currentDate = soup.find("span", class="posted-on")

I'm looking for the following element in the page:

<span class="posted-on">Posted on Friday, <br/>August 12th, 2011</span>

I'm getting this syntax error instead:

"test.py", line 22
currentDate = soup.find("span", class="posted-on")
                                    ^
SyntaxError: invalid syntax

The basic documentation online seems identical to me (obviously assuming find_parents() and find() work much the same way):

a_string.find_parent("p")
# <p class="story">Once upon a time there were three little sisters; and their names were
#  <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
#  and they lived at the bottom of a well.</p>

a_string.find_parents("p", class="title")
# []

So what am I doing wrong? I know class is a reserved Python keyword; is that what's messing this up somehow?

like image 300
strugglingcomic Avatar asked Dec 31 '12 20:12

strugglingcomic


1 Answers

You cannot use class as a keyword argument. Use {'class': 'posted-on'} instead:

currentDate = soup.find('span', {'class': 'posted-on'})

Alternatively, bs4 supports a class_ spelling too:

currentDate = soup.find('span', class_='posted-on')
like image 165
Martijn Pieters Avatar answered Oct 22 '22 16:10

Martijn Pieters