2009-03-02 40 views
4

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

回答

6

建议使用版本3.0.7a,如Łukasz。 BeautifulSoup 3.1被设计为与Python 3.0兼容,因此他们必须将解析器从SGMLParser更改为HTMLParser,而HTMLParser似乎更容易受到错误的HTML影响。

changelog for BeautifulSoup 3.1

“美丽的汤现在基于HTMLParser的,而不是SGMLParser中,这是走在Python 3有一些不好的HTML,就是SGMLParser处理,但HTMLParser的没有”

+0

这个位置的一些详细信息:HTTP:// WWW .crummy.com/software/BeautifulSoup/3.1-problems.html – FeatureCreep 2009-11-21 19:13:21

2

BeautifulSoup并不神奇:如果传入的HTML太可怕了,那么它就不起作用。

在这种情况下,传入的HTML就是这样的:对于BeautifulSoup来说弄不清楚该做什么。例如,它包含标记,如:

SCRIPT TYPE =“” JavaScript的“”

的BeautifulSoup文档包含一个部分,如果BeautifulSoup无法解析你能做什么(注意双引号)。你标记。您需要调查这些替代方案。

0

我在BeautifulSoup版本'3.0.7a'上测试了这个脚本,它返回了看起来是正确的输出。我不知道'3.0.7a'和'3.1.0.1'之间有什么变化,但尝试一下。

0
import urllib 
from BeautifulSoup import BeautifulSoup 

>>> page = urllib.urlopen('http://www.futureshop.ca/catalog/subclass.asp?catid=10607&mfr=&logon=&langid=FR&sort=0&page=1') 
>>> soup = BeautifulSoup(page) 
>>> soup.prettify() 

在我的情况下,通过执行上述语句,它返回整个HTML页面。

+0

在给任何人投票之前给出适当的理由。这将有点道德。哦!如果你不明白我的答案,那么上帝可以帮助你 – aatifh 2009-03-09 07:02:35

1

我有问题解析下面的代码太:

<script> 
     function show_ads() { 
      document.write("<div><sc"+"ript type='text/javascript'src='http://pagead2.googlesyndication.com/pagead/show_ads.js'></scr"+"ipt></div>"); 
     } 
</script> 

HTMLParseError:坏的结束标记:U '',在第26行,列127

山姆

4

尝试lxml。尽管它的名字,它也用于解析和刮取HTML。它比BeautifulSoup快得多,它甚至比BeautifulSoup处理“破碎的”HTML更好,所以它可能对你更好。如果您不想学习lxml API,它也具有用于BeautifulSoup的兼容性API。

Ian Blicking agrees

没有理由再使用BeautifulSoup,除非您使用的是Google App Engine或其他任何不是纯粹Python不允许的东西。

2

Samj:如果我得到的东西像 HTMLParser.HTMLParseError: bad end tag: u"</scr' + 'ipt>" 我刚刚从标记删除的罪魁祸首之前,我把它用来BeautifulSoup和所有为花花公子:

html = urllib2.urlopen(url).read() 
html = html.replace("</scr' + 'ipt>","") 
soup = BeautifulSoup(html)