我想用BeautifulSoup解析html页面,但看起来BeautifulSoup根本不喜欢html或那个页面。当我运行下面的代码时,prettify()方法只返回页面的脚本块(参见下文)。有人有一个想法,为什么会发生?BeautifulSoup解析的问题
import urllib2
from BeautifulSoup import BeautifulSoup
url = "http://www.futureshop.ca/catalog/subclass.asp?catid=10607&mfr=&logon=&langid=FR&sort=0&page=1"
html = "".join(urllib2.urlopen(url).readlines())
print "-- HTML ------------------------------------------"
print html
print "-- BeautifulSoup ---------------------------------"
print BeautifulSoup(html).prettify()
这是BeautifulSoup生成的输出。
-- BeautifulSoup ---------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript">
<!--
function highlight(img) {
document[img].src = "/marketing/sony/images/en/" + img + "_on.gif";
}
function unhighlight(img) {
document[img].src = "/marketing/sony/images/en/" + img + "_off.gif";
}
//-->
</script>
谢谢!
更新:我使用的是以下版本,它看起来是最新版本。
__author__ = "Leonard Richardson ([email protected])"
__version__ = "3.1.0.1"
__copyright__ = "Copyright (c) 2004-2009 Leonard Richardson"
__license__ = "New-style BSD"
这个位置的一些详细信息:HTTP:// WWW .crummy.com/software/BeautifulSoup/3.1-problems.html – FeatureCreep 2009-11-21 19:13:21