10

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?

4

1 回答 1

8

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')
于 2012-12-31T20:51:21.740 回答