我想用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