Mar*_*tin 4 python beautifulsoup
我试图用BeautifulSoup解析一个html页面,但看起来BeautifulSoup根本不喜欢html或那个页面.当我运行下面的代码时,方法prettify()只返回页面的脚本块(见下文).有人知道它为什么会发生吗?
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()
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
谢谢!
更新:我使用的是以下版本,这似乎是最新版本.
__author__ = "Leonard Richardson (leonardr@segfault.org)"
__version__ = "3.1.0.1"
__copyright__ = "Copyright (c) 2004-2009 Leonard Richardson"
__license__ = "New-style BSD"
Run Code Online (Sandbox Code Playgroud)
尝试使用版本3.0.7a作为Łukasz建议.BeautifulSoup 3.1旨在与Python 3.0兼容,因此他们必须将解析器从SGMLParser更改为HTMLParser,这似乎更容易受到不良HTML的攻击.
"Beautiful Soup现在基于HTMLParser而不是SGMLParser,它在Python 3中消失了.有一些不好的HTML,SGMLParser处理但是HTMLParser没有"
| 归档时间: |
|
| 查看次数: |
8334 次 |
| 最近记录: |