使用beautifulsoup.find奇怪的语法错误() [英] Weird syntax error in using beautifulsoup.find()
本文介绍了使用beautifulsoup.find奇怪的语法错误()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能完全是显而易见的,但我很为难(还挺新的Python,不好意思):
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
基本的在线文档似乎等同于我(假设显然find_parents()和find()工作大致相同的方式):
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")
# []
那么,我做错了什么?我知道类是保留的Python关键词;那是什么不知何故搞乱这个吗?
So what am I doing wrong? I know class is a reserved Python keyword; is that what's messing this up somehow?
推荐答案
您不能使用类
作为关键字参数。使用 {'类':'贴式'}
而不是:
You cannot use class
as a keyword argument. Use {'class': 'posted-on'}
instead:
currentDate = soup.find('span', {'class': 'posted-on'})
另外,BS4支持 类_
拼写太:
currentDate = soup.find('span', class_='posted-on')
这篇关于使用beautifulsoup.find奇怪的语法错误()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文