BeautifulSoup 找不到标记

BeautifulSoup Cannot Find Tag

我正在尝试抓取 this page 和所有其他类似的页面。我一直在使用 BeautifulSoup (也尝试过 lxml 但存在安装问题)。我正在使用以下代码:

value = "http://www.presidency.ucsb.edu/ws/index.php?pid=99556"
desiredTag = "span"
r = urllib2.urlopen(value)
data = BeautifulSoup(r.read(), 'html5lib') 
displayText = data.find_all(desiredTag)
print displayText
displayText = " ".join(str(displayText))
displayText = BeautifulSoup(displayText, 'html5lib')

出于某种原因,这并没有拉回 <span class="displaytext"> 而且我已经尝试 desiredTag 作为 p

我是不是漏掉了什么?

您肯定遇到了 BeautifulSoup 使用的差异 between different parsershtml.parserlxml 对我有用:

data = BeautifulSoup(urllib2.urlopen(value), 'html.parser') 

证明:

>>> import urllib2
>>> from bs4 import BeautifulSoup
>>> 
>>> url = "http://www.presidency.ucsb.edu/ws/index.php?pid=99556"
>>> 
>>> data = BeautifulSoup(urllib2.urlopen(url), 'html.parser')
>>> data.find("span", class_="displaytext").text
u'PARTICIPANTS:Former Speaker of the House Newt Gingrich (GA);
...