这可能是完全显而易见的,但我很难过(有点陌生,对不起):
page = urllib2.urlopen("http://www.somerandompage.com")
soup = BeautifulSoup(page)
currentDate = soup.find("span", class="posted-on")
我在页面中寻找以下元素:
<span class="posted-on">Posted on Friday, <br/>August 12th, 2011</span>
我收到了这种语法错误:
"test.py", line 22
currentDate = soup.find("span", class="posted-on")
^
SyntaxError: invalid syntax
在线基本文档看起来与我完全相同(显然假设find_parents()和find()的工作方式大致相同):
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")
# []
那么我做错了什么?我知道class是一个保留的Python关键字;是什么东西搞砸了?
答案 0 :(得分:7)
您不能将class
用作关键字参数。请改用{'class': 'posted-on'}
:
currentDate = soup.find('span', {'class': 'posted-on'})
或者,bs4也支持class_
spelling:
currentDate = soup.find('span', class_='posted-on')