BeautifulSoup无法找到标签

时间:2015-03-25 17:34:55

标签: python web-scraping beautifulsoup

我正在尝试抓取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

我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

您肯定遇到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);
...